企业库日志块滚动平面文件不写入文件

时间:2016-02-07 19:48:31

标签: c# asp.net-mvc logging asp.net-web-api enterprise-library

我正在实现一个多层Web解决方案,使用ASP.NET MVC 5作为Web堆栈,使用WebApi 2.2作为后端服务堆栈。这些将在部署时在不同的服务器上运行,但在本地运行两个不同的进程以进行开发 - Windows 7和IIS 7.5。我正在尝试使用Enterprise Library 6 Logging Application Block在两个进程中实现Rolling Flat File跟踪侦听器。我有一个包含我的Log类的公共项目,它将保留在它自己的单例实例中 - 所有对Logger的访问都将是:

public sealed class Log : ILog
{
    private static readonly Log m_log;

    static Log()
    {
        var factory = new LogWriterFactory();
        Logger.SetLogWriter(factory.Create());

        m_log = new Log();
    }

    private Log() // no one else can create
    { }

    public static ILog Instance { get { return m_log; } }

    public bool IsLoggingEnabled()
    {
        return Logger.IsLoggingEnabled();
    }

    public void Critical(object caller, string method, string message)
    {
        if (Logger.IsLoggingEnabled())
            Write(caller, method, message, TraceEventType.Critical);
    }

    public void Error(object caller, string method, string message)
    {
        if (Logger.IsLoggingEnabled())
            Write(caller, method, message, TraceEventType.Error);
    }

    public void Warning(object caller, string method, string message)
    {
        if (Logger.IsLoggingEnabled())
            Write(caller, method, message, TraceEventType.Warning);
    }

    public void Information(object caller, string method, string message)
    {
        if (Logger.IsLoggingEnabled())
            Write(caller, method, message, TraceEventType.Information);
    }

    private void Write(object caller, string method, string message, TraceEventType severity = TraceEventType.Information)
    {
        var entry = new LogEntry();
        entry.Severity = severity;

        if (Logger.ShouldLog(entry))
        {
            entry.TimeStamp = DateTime.Now;
            entry.Message = message;

            entry.ExtendedProperties.Add("Type", caller.GetType().Name);
            entry.ExtendedProperties.Add("Method", method);

            ...

            Logger.Write(entry);
        }
    }
}

这两个进程的配置完全相同,但写入的文件名除外(MVC写入Web.log,WebApi写入WebApi.log):

<loggingConfiguration name="loggingConfiguration" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<logFilters>
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
      enabled="true" name="Logging Enabled Filter" />
</logFilters>
<listeners>
  <add name="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"
    fileName="C:\Logs\WebApi.log"
    footer="" formatter="Text Formatter" header=""
    rollFileExistsBehavior="Increment" rollInterval="Midnight"
    timeStampPattern="yyyy-MM-DD" 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"
    template="Timestamp:{timestamp} Severity:{severity} Message:{message} Machine:{machine} ProcessId:{processId} ThreadId:{win32ThreadId}{dictionary( {key}:{value})}"
    name="Text Formatter" />
</formatters>
<categorySources>
  <add switchValue="All" name="General" autoFlush="true">
    <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>

WebApi堆栈正在使用Unity为所有控制器依赖项执行DI。我的单例实例正在被正确解析并注入到我的所有控制器派生的ApiController基类中。我有一个动作过滤器,它试图在onActionExecutingAsync和onActionExecutedAsync事件中写入日志。

问题在于:WebApi进程没有将条目写入WebApi.log文件。该过程创建文件;它只是没有在那里写任何东西。我很确定我在那里写了一些东西,但我不能让它在那里记录任何东西了。我已经逐步调试,IsLoggingEnabled()返回true,并调用Logger.Write方法。我已经尝试将所有引用撤消到前端的Logging Block,认为在同一台机器上的多个进程中运行块时出现问题。没变。前端MVC进程正好创建并记录到Web.log文件(同样的配置)。任何人都有任何关于我失踪的线索?提前谢谢。

1 个答案:

答案 0 :(得分:0)

日志应用程序块中有known issue,如果ExtendedProperty值为null,则条目LogEntry将失败并且不会被记录。

如果为任何ExtendedProperty值传入null,请尝试更改代码以写入空字符串。例如:

entry.ExtendedProperties.Add("Method", method ?? String.Empty);

这应避免遇到问题。