这是我写的第一个关于Logger的实例程序:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
namespace LoggerExample
{
class Program
{
static void Main(string[] args)
{
LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Sample message";
// logEntry.Categories.Add("UI Events");
// logEntry.Severity = TraceEventType.Information;
// logEntry.Priority = 5;
Logger.Write(logEntry);
Console.WriteLine("The logger has been recorded.");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
namespace LoggerExample
{
class Program
{
static void Main(string[] args)
{
LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Sample message";
// logEntry.Categories.Add("UI Events");
// logEntry.Severity = TraceEventType.Information;
// logEntry.Priority = 5;
Logger.Write(logEntry);
Console.WriteLine("The logger has been recorded.");
Console.ReadLine();
}
}
}
其中参考了:http://blog.csdn.net/snlei/archive/2008/03/07/2155925.aspx
http://www.devx.com/dotnet/Article/36184/0/page/1
http://blog.csdn.net/flyingdream123/archive/2009/01/31/3855542.aspx
还有TerryLee的文章。
重点不在程序如何去写,关键是配置文件,使用微软自带的工具,可以很方便的编辑配置文件:
Code
1<configuration>
2 <configSections>
3 <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
4 </configSections>
5 <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
6 defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
7 <listeners>
8 <add fileName="MyTrace.log" header="----------------------------------------"
9 footer="----------------------------------------" formatter="Text Formatter"
10 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
11 traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
12 name="FlatFile TraceListener" />
13 <add source="Enterprise Library Logging" formatter="Text Formatter"
14 log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
15 traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
16 name="EventLog TraceListener" />
17 </listeners>
18 <formatters>
19 <add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
20 type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
21 name="Text Formatter" />
22 </formatters>
23 <logFilters>
24 <add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
25 name="My Category Filters" />
26 </logFilters>
27 <categorySources>
28 <add switchValue="All" name="General">
29 <listeners>
30 <add name="FlatFile TraceListener" />
31 <add name="EventLog TraceListener" />
32 </listeners>
33 </add>
34 </categorySources>
35 <specialSources>
36 <allEvents switchValue="All" name="All Events" />
37 <notProcessed switchValue="All" name="Unprocessed Category" />
38 <errors switchValue="All" name="Logging Errors & Warnings">
39 <listeners>
40 <add name="FlatFile TraceListener" />
41 </listeners>
42 </errors>
43 </specialSources>
44 </loggingConfiguration>
45</configuration>
1<configuration>
2 <configSections>
3 <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
4 </configSections>
5 <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
6 defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
7 <listeners>
8 <add fileName="MyTrace.log" header="----------------------------------------"
9 footer="----------------------------------------" formatter="Text Formatter"
10 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
11 traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
12 name="FlatFile TraceListener" />
13 <add source="Enterprise Library Logging" formatter="Text Formatter"
14 log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
15 traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
16 name="EventLog TraceListener" />
17 </listeners>
18 <formatters>
19 <add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
20 type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
21 name="Text Formatter" />
22 </formatters>
23 <logFilters>
24 <add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
25 name="My Category Filters" />
26 </logFilters>
27 <categorySources>
28 <add switchValue="All" name="General">
29 <listeners>
30 <add name="FlatFile TraceListener" />
31 <add name="EventLog TraceListener" />
32 </listeners>
33 </add>
34 </categorySources>
35 <specialSources>
36 <allEvents switchValue="All" name="All Events" />
37 <notProcessed switchValue="All" name="Unprocessed Category" />
38 <errors switchValue="All" name="Logging Errors & Warnings">
39 <listeners>
40 <add name="FlatFile TraceListener" />
41 </listeners>
42 </errors>
43 </specialSources>
44 </loggingConfiguration>
45</configuration>