logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
//只能设置一个
logEntry.Categorys = "UI Events";
Logger.Write(logEntry);
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
//设置多个Category
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");
Logger.Write(logEntry);
3.可以在代码中查询哪些日志项将被过滤,例如:
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.
}
4.配置文件的改变。在1.1下关于Logging & Instrumentation Application Block的信息记录在loggingconfiguration.config文件中,2.0下所有的信息都放在了Web.config或App.config中,如:
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
</configSections>
<loggingConfiguration tracingEnabled="true" defaultCategory="General">
<logFilters>
<add
name="Category"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
categoryFilterMode="AllowAllExceptDenied">
<categoryFilters>
<add name="UI Events" />
</categoryFilters>
</add>
<add
name="Priority"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
minimumPriority="2"
/>
<add name="LogEnabled Filter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
enabled="true"
/>
</logFilters>
</loggingConfiguration>
</configuration>
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
2.创建一个日志项
log.EventId = 300;
log.Message = "Sample message";
log.Categories.Add("UI Events");
log.Severity = TraceEventType.Information;
log.Priority = 5;
3.调用Logger.Write()方法
// 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);
{
DoDataAccess();
}
{
using (new Tracer("Data Access Events"))
{
// Peform work here
// Assume an error condition was detected - perform some troubleshooting.
DoTroubleShooting();
}
}
{
string logMessage = "Simulated troubleshooting message for Logging QuickStart. " +
"Current activity=\"" + Trace.CorrelationManager.ActivityId + "\"";
LogEntry logEntry = new LogEntry();
logEntry.Categories.Clear();
logEntry.Categories.Add("Troubleshooting");
logEntry.Priority = 5;
logEntry.Severity = TraceEventType.Error;
logEntry.Message = logMessage;
Logger.Write(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.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.
}
public class DebugTraceListener : CustomTraceListener
{
//
}
{
if (data is LogEntry && this.Formatter != null)
{
this.WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
this.WriteLine(data.ToString());
}
}
/// Writes a message to the debug window
/// </summary>
/// <param name="message">The string to write to the debug window</param>
public override void 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>
public override void WriteLine(string message)
{
Debug.WriteLine(message);
}
public class MyFormatter : ILogFormatter
{
//
}
{
//
}
{
//
}