gpt4 book ai didi

asp.net - 在asp.net中使用Enterprise Library实现日志文件

转载 作者:行者123 更新时间:2023-12-02 16:27:31 24 4
gpt4 key购买 nike

我在asp.net中使用Microsoft Enterprise Library 3.1进行异常处理,错误存储在系统的事件查看器中。

我需要使用企业库将这些错误存储在日志文件中,而不是事件查看器。

最佳答案

亲爱的2:30,您将以下代码粘贴到app.config或web.config文件的配置部分

  <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 &amp; Warnings" />
</specialSources>
</loggingConfiguration>

可以使用以下语句将应用程序日志记录到applog.log文件中

Logger.Write("Application Started", "AppLog");

可以使用以下语句将应用程序异常记录到 Exception.log 文件中

Logger.Write("Error: Invalid information you passed", "ExceptionHandling");

注意:您可以在 Microsoft.Practices.EnterpriseLibrary.Logging 中找到 Logger 类;

AppLog.log和Exception.log文件将自动在bin文件夹中创建。

关于asp.net - 在asp.net中使用Enterprise Library实现日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1503582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com