//=============================================================================== // Microsoft Exception Management Application Block for .NET // http://msdn.microsoft.com/library/en-us/dnbda/html/emab-rm.asp // // BaseApplicationException.cs // This file contains the definition for the BaseApplicationException. // For more information see the Implementing the Base Application Exception Class // section of the Exception Management Application Block Implementation Overview. //=============================================================================== // Copyright (C) 2000-2001 Microsoft Corporation // All rights reserved. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR // FITNESS FOR A PARTICULAR PURPOSE. //=============================================================================== using System; using System.Collections.Specialized; using System.Reflection; using System.Resources; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using System.Security.Principal; using System.Threading; namespace AyAEM.ExceptionManagement { /// /// Base Application Exception Class. You can use this as the base exception object from /// which to derive your applications exception hierarchy. /// [Serializable] public class BaseApplicationException : ApplicationException { #region Constructors /// /// Constructor with no params. /// public BaseApplicationException() : base() { InitializeEnvironmentInformation(); } /// /// Constructor allowing the Message property to be set. /// /// String setting the message of the exception. public BaseApplicationException(string message) : base(message) { InitializeEnvironmentInformation(); } /// /// Constructor allowing the Message and InnerException property to be set. /// /// String setting the message of the exception. /// Sets a reference to the InnerException. public BaseApplicationException(string message,Exception inner) : base(message, inner) { InitializeEnvironmentInformation(); } /// /// Constructor used for deserialization of the exception class. /// /// Represents the SerializationInfo of the exception. /// Represents the context information of the exception. protected BaseApplicationException(SerializationInfo info, StreamingContext context) : base(info, context) { machineName = info.GetString("machineName"); createdDateTime = info.GetDateTime("createdDateTime"); appDomainName = info.GetString("appDomainName"); threadIdentity = info.GetString("threadIdentity"); windowsIdentity = info.GetString("windowsIdentity"); additionalInformation = (NameValueCollection)info.GetValue("additionalInformation",typeof(NameValueCollection)); } #endregion #region Declare Member Variables // Member variable declarations private string machineName; private string appDomainName; private string threadIdentity; private string windowsIdentity; private DateTime createdDateTime = DateTime.Now; private static ResourceManager resourceManager = new ResourceManager(typeof(ExceptionManager).Namespace + ".ExceptionManagerText",Assembly.GetAssembly(typeof(ExceptionManager))); // Collection provided to store any extra information associated with the exception. private NameValueCollection additionalInformation = new NameValueCollection(); #endregion /// /// Override the GetObjectData method to serialize custom values. /// /// Represents the SerializationInfo of the exception. /// Represents the context information of the exception. [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData( SerializationInfo info, StreamingContext context ) { info.AddValue("machineName", machineName, typeof(string)); info.AddValue("createdDateTime", createdDateTime); info.AddValue("appDomainName", appDomainName, typeof(string)); info.AddValue("threadIdentity", threadIdentity, typeof(string)); info.AddValue("windowsIdentity", windowsIdentity, typeof(string)); info.AddValue("additionalInformation", additionalInformation, typeof(NameValueCollection)); base.GetObjectData(info,context); } #region Public Properties /// /// Machine name where the exception occurred. /// public string MachineName { get { return machineName; } } /// /// Date and Time the exception was created. /// public DateTime CreatedDateTime { get { return createdDateTime; } } /// /// AppDomain name where the exception occurred. /// public string AppDomainName { get { return appDomainName; } } /// /// Identity of the executing thread on which the exception was created. /// public string ThreadIdentityName { get { return threadIdentity; } } /// /// Windows identity under which the code was running. /// public string WindowsIdentityName { get { return windowsIdentity; } } /// /// Collection allowing additional information to be added to the exception. /// public NameValueCollection AdditionalInformation { get { return additionalInformation; } } #endregion /// /// Initialization function that gathers environment information safely. /// private void InitializeEnvironmentInformation() { try { machineName = Environment.MachineName; } catch(SecurityException) { machineName = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"); } catch { machineName = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"); } try { threadIdentity = Thread.CurrentPrincipal.Identity.Name; } catch(SecurityException) { threadIdentity = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"); } catch { threadIdentity = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"); } try { windowsIdentity = WindowsIdentity.GetCurrent().Name; } catch(SecurityException) { windowsIdentity = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"); } catch { windowsIdentity = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"); } try { appDomainName = AppDomain.CurrentDomain.FriendlyName; } catch(SecurityException) { appDomainName = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"); } catch { appDomainName = resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"); } } } }