Using the logging Application module of Enterprise Library 6.0, the configuration steps are as follows:
1.Nuget installation Enterprise Library logging module
Command line: Install-package enterpriselibrary.logging.
2. Configuration file:
The current basic is through Enterprise Library configuration, but unfortunately, I always install failure, so I Baidu A, and then configure, configured as follows:
<configsections> < Sectionname= "Loggingconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging "/> </configsections> <loggingconfigurationname=""tracingenabled= "true"defaultcategory= "General"> <Listeners> <Addname= "Rolling Flat File Trace Listener"type= "Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging "Listenerdatatype= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging "Formatter= "Text Formatter"Rollinterval= "Day"rollsizekb= "1024x768"FileName= "App_data/trace.log"Maxarchivedfiles= "Ten" /> </Listeners> <formatters> <Addtype= "Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging "Template= "Timestamp: {timestamp}{newline}& #xA; title:{title}{newline}& #xA; severity:{severity}{newline}& #xA; Message: {message}{newline}& #xA; Extended Properties: {Dictionary ({key}-{value}{newline})} "name= "Text Formatter" /> </formatters> <categorysources> <AddSwitchvalue= "All"name= "General"> <Listeners> <Addname= "Rolling Flat File Trace Listener" /> </Listeners> </Add> </categorysources> <specialsources> <alleventsSwitchvalue= "All"name= "All Events" /> <notprocessedSwitchvalue= "All"name= "Unprocessed Category" /> <ErrorsSwitchvalue= "All"name= "Logging Errors & Warnings "> </Errors> </specialsources> </loggingconfiguration>
3. Simple package logger, as follows:
Public Sealed classLoghelper {Private StaticLoghelper instance =NULL; Static ReadOnly ObjectLockobj =New Object(); Public Staticloghelper Instance {Get { if(Instance = =NULL) { Lock(lockobj) {if(instance==NULL) {instance=NewLoghelper (); } } } returninstance; } } PrivateLogWriter _logwriter =NULL; PrivateLoghelper () {Logwriterfactory factory=Newlogwriterfactory (); _logwriter=Factory. Create (); } Private voidLogcontent (System.Diagnostics.TraceEventType Tracetype,stringTitlestringmessage) { if(_logwriter.isloggingenabled ()) {LogEntry entity=NewLogEntry (); Entity. Severity=Tracetype; Entity. TimeStamp=DateTime.Now; Entity. Message=message; Entity. Title=title; _logwriter.write (entity); } } /// <summary> ///Log Exception infomation/// </summary> /// <param name= "title" >Log title</param> /// <param name= "ex" >Exception</param> Public voidlogexception (Exception ex) {logcontent (System.Diagnostics.TraceEventType.Error, ex. Message, ex. ToString ()); }
4. After testing, write the log.
Enterprise Library can refer to the Official document: Http://msdn.microsoft.com/zh-cn/library/dn169621.aspx
Enterprise Library 6.0 Semantic Logging application Block Configuration