This is the first logger instance I wrote.Program:
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 = 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 ();
}
}
}
The reference: 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
And terrylee'sArticle.
The focus is not on how the program is written. The key is the configuration file. You can easily edit the configuration file by using Microsoft's built-in tools:
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. Conflict, Microsoft. Practices. incluiselibrary. Logging, version = 2.0.0.0, culture = neutral, publickeytoken = NULL"
16 Name = "EventLog tracelistener" />
17 </ Listeners >
18 < Formatters >
19 < Add Template = "Timestamp: {timestamp} & # XD; & # XA; message: {message} & # XD; & # XA; Category: {category} & # XD; & # XA; Priority: {priority} & # XD; & # XA; eventid: {eventid} & # XD; & # XA; Severity: {severity} & # XD; & # XA; Title: {Title} & # XD; & # XA; machine: {MACHINE} & # XD; & # XA; application domain: {appdomain} & # XD; & # XA; process ID: {processid} & # XD; & # XA; process name: {processname} & # XD; & # XA; Win32 thread ID: {win32threadid} & # XD; & # XA; thread name: {threadname} & # XD; & # XA; extended properties: {Dictionary ({key}-{value} & # XD; & # XA ;)}"
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 & amp; warnings" >
39 < Listeners >
40 < Add Name = "Flatfile tracelistener" />
41 </ Listeners >
42 </ Errors >
43 </ Specialsources >
44 </ Loggingconfiguration >
45 </ Configuration >