Semplice logging su file :
1: LogEntry logEntry = new LogEntry();
2: logEntry.Message = "Test logging message";
3: logEntry.Severity = System.Diagnostics.TraceEventType.Error;
4: logEntry.ExtendedProperties.Add("Class", "Program");
5: logEntry.ExtendedProperties.Add("Method", "Main");
6: Logger.Write(logEntry);
App.config (contenente link a file di config esterno):
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <configSections>
4: <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
5: </configSections>
6: <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
7: <sources>
8: <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
9: filePath="EntLib.config" />
10: </sources>
11: </enterpriseLibrary.ConfigurationSource>
12: </configuration>
EntLib.config (file di configurazione esterno):
1: <?xml version="1.0" encoding="utf-8"?>
2: <configuration>
3: <configSections>
4: <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
5: <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
6: </configSections>
7: <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
8: defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
9: <listeners>
10: <add fileName="C:\logbase.log" header="----------------------------------------"
11: footer="----------------------------------------" formatter="Text Formatter"
12: listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
13: traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
14: name="FlatFile TraceListener" />
15: </listeners>
16: <formatters>
17: <add template="Timestamp: {timestamp}
Message: {message}
Severity: {severity}
Title:{title}
{dictionary({key}: {value}
)}"
18: type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
19: name="Text Formatter" />
20: </formatters>
21: <specialSources>
22: <allEvents switchValue="All" name="All Events">
23: <listeners>
24: <add name="FlatFile TraceListener" />
25: </listeners>
26: </allEvents>
27: <notProcessed switchValue="All" name="Unprocessed Category" />
28: <errors switchValue="All" name="Logging Errors & Warnings" />
29: </specialSources>
30: </loggingConfiguration>
31: </configuration>
Output (c:\logbase.log):
—————————————-
Timestamp: 09/06/2009 13:02:08
Message: Test logging message
Severity: Error
Title:
Class: Program
Method: Main
—————————————-