使用asp.net中的企业库实现日志文件 [英] Implementing Log file using Enterprise Library in asp.net
问题描述
我在asp.net中使用Microsoft Enterprise Library 3.1进行异常处理时,错误存储在系统的 Event Viewer 中。
I m using Microsoft Enterprise Library 3.1 for Exception Handling in asp.net, the erors are stored in Event Viewer of the system .
我需要使用企业库将这些错误存储在日志文件中,而不是事件查看器。
Instead of Event Viewer I need to store these errors in a log File using Enterprise Library.
推荐答案
亲爱的2:30,您将以下代码粘贴到app.config或web.config文件的配置部分中:
Dear 2:30, You paste the following code inside configuration section at app.config or web.config file
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Tracing" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="AppLog.log" rollSizeKB="1024" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="None" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="LogicalOperationStack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="AppLog" />
<add fileName="Exception.log" rollSizeKB="1024" timeStampPattern="MM-dd-yyyy" rollFileExistsBehavior="Increment" rollInterval="None" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="Callstack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Exception" />
<add fileName="trace.log" rollSizeKB="1024" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollInterval="Month" formatter="Text Formatter" header="----------------------------------------" footer="----------------------------------------" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Trace" />
</listeners>
<formatters>
<add template="{timestamp} : {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="AppLog">
<listeners>
<add name="AppLog" />
</listeners>
</add>
<add switchValue="Verbose" name="ExceptionHandling">
<listeners>
<add name="Exception" />
</listeners>
</add>
<add switchValue="Information" name="Tracing">
<listeners>
<add name="Trace" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="Off" name="Logging Errors & Warnings" />
</specialSources>
</loggingConfiguration>
应用程序日志可以使用以下语句登录到applog.log文件中
Application log can be logged into applog.log file using following statement
Logger.Write("Application Started", "AppLog");
应用程序异常可以使用以下语句登录到Exception.log文件中
Application exception can be logged into Exception.log file using following statement
Logger.Write("Error: Invalid information you passed", "ExceptionHandling");
注意:您可以在Microsoft.Practices.EnterpriseLibrary.Logging;中找到Logger类。
Note: You can find the Logger class in Microsoft.Practices.EnterpriseLibrary.Logging;
AppLog.log和Exception.log文件将自动在bin文件夹中创建。
AppLog.log and Exception.log file will be created in bin folder automatically.
这篇关于使用asp.net中的企业库实现日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!