1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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