Microsoft EnterpriseLibrary日志记录:未生成日志

时间:2016-11-09 05:01:35

标签: c# asp.net-mvc enterprise-library-5

我正在开发ASP.net MVC项目,需要使用Microsoft.Practices.EnterpriseLibrary.Logging(即Microsoft Enterprise Library 5.0)在TEXT文件中记录条目。

问题:LOGGING无效。我正在写一个文本文件中的日志,但没有条目进入文本文件。我已经提到了配置文件和Controller的代码片段,如下所述。 我只需要使用M S Enterprise Library。

请提出此问题的任何可行解决方案。

代码段: Web.config

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  </configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Architecture" logWarningsWhenNoCategoriesMatch="false" revertImpersonation="false">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          source="Enterprise Library Logging" formatter="Text Formatter"
          log="LuisLogging" machineName="." traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ThreadId, Callstack" />

      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             fileName="./File/loggingLuis.log" formatter="Text Formatter" />
    </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}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
          name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

控制器代码:

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Unity;
using Microsoft.Practices.EnterpriseLibrary.Common;

try
            {
                var fileContent = Request.Files["HeaderTemplateView"];
                Logger.Write("Hello World another time: Start");
                Logger.Write("Hello World another time: End");
                LogWriterFactory logWriterFactory = new LogWriterFactory();
                LogWriter logWriter = logWriterFactory.Create();
                logWriter.Write("Hello by LogWriter");
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Error Message:{0} , InnerEx : {1}", ex.Message, (ex.InnerException != null) ? ex.InnerException.ToString() : string.Empty), "UploadServiceError");
            }

0 个答案:

没有答案