c# - 企业库记录 block 滚动平面文件不写入文件

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

我正在实现一个多层网络解决方案,使用 ASP.NET MVC 5 作为网络堆栈,使用 WebApi 2.2 作为后端服务堆栈。这些将在部署时位于不同的服务器上,但在本地运行在两个不同的进程中以进行开发 - Windows 7 和 IIS 7.5。我正在尝试使用 Enterprise Library 6 Logging Application Block 在两个进程中实现滚动平面文件跟踪监听器。我有一个包含我的 Log 类的公共(public)项目,该类将保留其自身的单例实例 - 对 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 为所有 Controller 依赖项执行 DI。我的单例实例被正确解析并注入(inject)到我的所有 Controller 都派生自的 ApiController 基类中。我有一个操作过滤器,它试图在 onActionExecutingAsync 和 onActionExecutedAsync 事件中写入日志。

问题在于:WebApi 进程未将条目写入 WebApi.log 文件。该过程创建文件;它只是没有在那里写任何东西。我很确定我一开始就在那里写了一些东西,但我就是不能再在那里记录任何东西了。我已经通过了调试器,IsLoggingEnabled() 返回 true,并且调用了 Logger.Write 方法。我已经尝试撤消对前端日志记录 block 的所有引用,认为在同一台机器上的多个进程中运行该 block 存在问题。不用找了。前端 MVC 进程正在创建并记录到 Web.log 文件(同样,相同的配置)。任何人对我所缺少的东西有任何线索吗?提前致谢。

最佳答案

有一个known issue使用日志记录应用程序 block ,如果 ExtendedProperty 值为空,则条目 LogEntry 将失败并且不会被记录。

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

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

这应该可以避免遇到问题。

关于c# - 企业库记录 block 滚动平面文件不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258246/

相关文章:

c# - 如何防止类 'a' 被另一个类继承?

C# 十六进制转ip

asp.net-mvc - ASP.NET MVC Identity - 如何更改 SMS 中生成的 token 的格式

javascript - 页面加载时地理位置将值传递给 url.action

ios - Lumberjack 日志 DDLogInfo 在控制台中显示为警告

C# 索引超出了数组范围

c# - 具有 128 位键的基于时间的字典/哈希表,即超时字典中的值

c# - 向我的应用程序添加服务层

logging - 如何在 Dart 中导入日志库?

ios - os_log : Unexpected value for integer in Console. 应用程序和 `log stream`