//****************************************************************************************************** // ErrorModule.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 04/17/2007 - Pinal C. Patel // Generated original version of source code. // 09/13/2007 - Darrell Zuercher // Edited code comments. // 09/24/2008 - Pinal C. Patel // Converted code to C#. // 10/17/2008 - Pinal C. Patel // Edited code comments. // 08/05/2009 - Josh L. Patterson // Edited Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Diagnostics; using System.Web; namespace GSF.ErrorManagement { /// /// Represents an HTTP module that can be used to handle exceptions globally in Web Sites and Web Services. /// /// /// Below is the config file entry required for enabling error handling using : /// /// /// /// /// /// /// /// /// ]]> /// /// Below is the config file entry required for changing the settings of : /// /// /// ///
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ]]> /// /// /// public class ErrorModule : IHttpModule { #region [ Members ] // Fields private HttpApplication m_context; #endregion #region [ Methods ] /// /// Initializes the and prepares it to handle requests. /// /// The to use for initialization. public void Init(HttpApplication context) { if (Logger.HandleUnhandledException && !Debugger.IsAttached) { m_context = context; // Save reference to the application. m_context.Error += OnError; // Register to be notified for unhandled exceptions. } } /// /// Disposes the resources (other than memory) used by the . /// public void Dispose() { if (Logger.HandleUnhandledException && !Debugger.IsAttached) { m_context.Error -= OnError; // Unregister from being notified for unhandled exceptions. } } private void OnError(object sender, EventArgs e) { // Log the last encountered exception. Logger.Log(HttpContext.Current.Server.GetLastError()); } #endregion #region [ Static ] private static readonly ErrorLogger s_logger; static ErrorModule() { s_logger = new ErrorLogger(); s_logger.PersistSettings = true; s_logger.Initialize(); // Initialize error logger for use. } /// /// Gets the object used by the object for logging exceptions. /// /// /// property can be used for logging handled exception throughout the web application. /// /// /// This example shows the use of for logging handled exception: /// /// using System; /// using GSF.ErrorManagement; /// /// namespace WebApp /// { /// public partial class _Default : System.Web.UI.Page /// { /// protected void Page_Load(object sender, EventArgs e) /// { /// try /// { /// string s = null; /// s.ToCharArray(); // This will result in NullReferenceException. /// } /// catch (Exception ex) /// { /// ErrorModule.Logger.Log(ex); // Log the encountered exception. /// } /// } /// } /// } /// /// public static ErrorLogger Logger { get { return s_logger; } } #endregion } }