LogEntry logEntry =new LogEntry(); logEntry.Priority =2; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); if (Logger.ShouldLog(logEntry)) { // Event will be logged according to currently configured filters. } else { // Event will not be logged. }
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging; using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation; using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
// Create the dictionary to hold the extra information, and populate it // with managed security information. Dictionary<string, object> dictionary =new Dictionary<string, object>(); ManagedSecurityContextInformationProvider informationHelper =new ManagedSecurityContextInformationProvider(); informationHelper.PopulateDictionary(dictionary); // Add a custom property for screen resolution int width = Screen.PrimaryScreen.Bounds.Width; int height = Screen.PrimaryScreen.Bounds.Height; string resolution = String.Format("{0}x{1}", width, height); dictionary.Add("Screen resolution", resolution); // Write the log entry that contains the extra information Logger.Write("Log entry with extra information", dictionary);
四.跟踪活动并记录上下文信息
1.调用DoDataAccess方法,完成后释放Trace对象
using (new Tracer("Trace")) { DoDataAccess(); }
2.创建DoDataAccess方法
privatevoid DoDataAccess() { using (new Tracer("Data Access Events")) { // Peform work here // Assume an error condition was detected - perform some troubleshooting. DoTroubleShooting(); } }
LogEntry logEntry =new LogEntry(); logEntry.Priority =2; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); if (Logger.GetFilter<CategoryFilter>().ShouldLog(logEntry)) { // Event will be logged according to currently configured filters. // Perform operations (possibly expensive) to gather additional information // for the event to be logged. } else { // Event will not be logged. Your application can avoid the performance // penalty of collecting information for an event that will not be // logged. }
LogEntry logEntry =new LogEntry(); logEntry.Priority =2; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); if (Logger.ShouldLog(logEntry)) { // Event will be logged according to currently configured filters. } else { // Event will not be logged. }
publicoverridevoid TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { if (data is LogEntry &&this.Formatter !=null) { this.WriteLine(this.Formatter.Format(data as LogEntry)); } else { this.WriteLine(data.ToString()); } }
3.覆写Write()和WriteLine()方法
/**////<summary> /// Writes a message to the debug window ///</summary> ///<param name="message">The string to write to the debug window</param> publicoverridevoid Write(string message) { Debug.Write(message); } /**////<summary> /// Writes a message to the debug window ///</summary> ///<param name="message">The string to write to the debug window</param> publicoverridevoid WriteLine(string message) { Debug.WriteLine(message); }