using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Diagnostics; using Microsoft.Practices.EnterpriseLibrary.Logging; using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation; using Microsoft.Practices.EnterpriseLibrary.Logging.Filters; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using System.Reflection; using System.Collections; namespace DemoServices.Logging { public class LogManager { public static void Write(Exception ex, string title = "", string category = "", TraceEventType severity = TraceEventType.Error) { if (ex != null) { if (String.IsNullOrEmpty(title)) { title = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name; } if (String.IsNullOrEmpty(category)) { category = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name; } Dictionary<string, object> props = null; if (ex.Data != null && ex.Data.Count < 0) { props = new Dictionary<string, object>(); foreach(string key in ex.Data) { props.Add(key, ex.Data[key]); } } Write(ex.ToString(), title, category, severity, props); } } public static void Write(string message, string title = "", string category = "", TraceEventType severity = TraceEventType.Error, IDictionary<string, object> props = null) { if (String.IsNullOrEmpty(title)) { title = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name; } if (String.IsNullOrEmpty(category)) { category = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name; } if (props == null) { props = new Dictionary<string, object>(); } ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider(); informationHelper.PopulateDictionary(props); DebugInformationProvider debugHelper = new DebugInformationProvider(); debugHelper.PopulateDictionary(props); LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<logwriter>(); logger.Write(message, new string[] { "Demo", category }, 20, 0, severity, title, props as Dictionary<string, object>); } //public static void Write(Exception ex, string title, string category, TraceEventType severity = TraceEventType.Error, Dictionary<string, object> props = null) //{ // if (ex != null) // { // Write(ex.ToString(), title, category, severity, props); // } //} //public static void Write(string message, string title, string category, TraceEventType severity = TraceEventType.Error) //{ // Dictionary<string, object> props = new Dictionary<string, object>(); // //props.Add("CallingFunction", (new StackTrace()).GetFrame(1).GetMethod().Name); // if (props == null) // { // props = new Dictionary<string, object>(); // } // ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider(); // informationHelper.PopulateDictionary(props); // DebugInformationProvider debugHelper = new DebugInformationProvider(); // debugHelper.PopulateDictionary(props); // LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<logwriter>(); // logger.Write(message, new string[] { "Demo", category }, 20, 0, severity, title, props); //} } }
Wednesday, February 15, 2012
Log Manager With Extra Information Using Enterprise Library
Here is a pattern for logging which encourages extra information to be stored with the error.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment