asp.net - 使用企业库创建滚动平面日志文件

标签 asp.net logging enterprise-library

在我的 Web 应用程序中,我试图创建一个日志文件来记录错误和异常,但是当我运行我的应用程序时,没有在我的解决方案文件夹或 bin 文件夹中创建日志文件。

我使用了以下代码。请帮我解决这个问题,在此先感谢您。

使用的命名空间

using Microsoft.Practices.EnterpriseLibrary.Logging;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

.cs 文件
        public int GetdeskKMasterRecordsCount(string CircleId, string StoreId)
        {
            try
            {
               throw new Exception("this is normal exception");

            }
            catch (Exception ex)
            {
                BindLog(ex);
                return 0;
            }
        }


        public static void BindLog(Exception ex)
        {
            if (ex == null) return;
            Logger.Write(LogInformation(1, DateTime.Now, ex.Message, " "));

        }

        public static LogEntry LogInformation(int eventId, DateTime timeStamp, string message, string documentName)
        {
            LogEntry logEntryObject = new LogEntry();
            logEntryObject.EventId = eventId;
            logEntryObject.Title = documentName;
            logEntryObject.TimeStamp = timeStamp;
            logEntryObject.MachineName = System.Environment.MachineName;
            logEntryObject.Message = message;
            logEntryObject.Categories.Add("Exception");

            return logEntryObject;
        }

Web 配置文件
<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="recordingWindowGroup" type="Vodafone.DMS.BAL.WindowConfigurationHandler, Vodafone.DMS.BAL"/>
    <section name="defaultParamGroup" type="Vodafone.DMS.BAL.DefaultParamConfiguration, Vodafone.DMS.BAL"/>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Rolling Flat File Trace Listener"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           fileName="Vodafone.DMS.log.exclude" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment"
           rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/>
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/>
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener"/>
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

最佳答案

企业库version 5.0 and above ,微软发布了企业库配置tool允许您在 configuration file 中直观地更改配置.您还可以从 NuGet 安装此工具,并且可以从 Visual Studio 内部访问该工具 Tools >> Library Package Manager >> Manage NuGet Packages for solution 并添加适当的 EntLib Application Block .您可以从 here 找到指南.

使用该工具,我生成了一个配置文件,如下所示:

将此放在 Web.Config 或 App.Config 文件中

  <loggingConfiguration name="loggingConfiguration" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="RollingFlatFile.log"
        footer="---------------------------" formatter="Text Formatter"
        header="---------------------------" rollFileExistsBehavior="Increment"
        rollInterval="Week" rollSizeKB="20000" timeStampPattern="yyyy-MM-dd hh:mm:ss"
        maxArchivedFiles="7" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
        filter="All" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

我建议不要在应用程序的 bin 目录中生成日志文件,而应该通过更改 的值来指定物理位置。 fileName="RollingFlatFile.log" 类似于 fileName="c:\logs\RollingFlatFile.log"

关于asp.net - 使用企业库创建滚动平面日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811005/

相关文章:

asp.net - 我如何确定这个 javascript 来自哪里?

asp.net - 使用 vb.net for vs2010 显示 .ppt/.pptx 文件

javascript - 如何将 CSS(特别是 padding/margin )应用于选项或选择下拉列表的选项组?

python - Python 日志记录中的准确时间戳

c# - 异常。EntLib 日志中缺少数据信息

asp.net - 将tracelistener添加到web.config

java - Spring 集成异常堆栈跟踪像 Apache Camel 一样?

azure - 如何在 azure 日志警报的数据透视后对 kusto 查询中的列进行索引

enterprise-library - 获取 DatabaseFactory.GetDatabase().ExecuteReader() 使用的连接

database - 企业图书馆缓存到本地数据库