EntLib V6在Windows服务中记录

时间:2015-11-20 09:09:06

标签: .net logging enterprise-library

我有一个使用ELAB作为日志记录的Windows服务。在控制台模式下运行服务时,所有日志记录都可以。但是一旦我作为服务启动它就不会发生日志记录。

我检查过的事情:

  • 检查了文件夹权限和
  • 从服务使用的用户
  • 以控制台模式运行服务
  • 使用PROCMON检查是否尝试创建日志文件
  • 使用绝对路径和相对路径来指定日志文件

我不知道为什么这不起作用。

初始化日志记录:

try
{
    var config = new SystemConfigurationSource(true, 30000);
    var logWriterFactory = new LogWriterFactory(config);
    LogWriter = logWriterFactory.Create();
    Logger.SetLogWriter(LogWriter, false);
}
catch (Exception ex)
{
    var msg = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Error loading configuration. {ex}";
    File.WriteAllText(Environment.ExpandEnvironmentVariables("%TEMP%\\mayflower_logging_error.txt"), msg);
    Debug.WriteLine(msg);
}

我的ELAB配置:

<enterpriseLibrary.ConfigurationSource selectedSource="Mayflower">
    <sources>
        <add name="Mayflower"
             type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </sources>
</enterpriseLibrary.ConfigurationSource>
<loggingConfiguration name="ServiceLogging" tracingEnabled="true" defaultCategory="General"
                      logWarningsWhenNoCategoriesMatch="false" revertImpersonation="false">
    <listeners>
        <add name="Logfile"
             type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             fileName="Logs\Mayflower-Server.log" footer="::: Message End :::"
             formatter="Detailed" header="" rollFileExistsBehavior="Increment"
             rollInterval="Midnight" rollSizeKB="50000" traceOutputOptions="LogicalOperationStack, DateTime"
             asynchronous="true" />
        <add
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            type="Mayflower.Server.Core.ConsoleTraceListener, Mayflower.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            filter="Information" name="Console" formatter="Short" />
    </listeners>
    <formatters>
        <add
            type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="{timestamp} {category} {message}{newline}    Priority: {priority} EventId: {eventid} Severity: {severity} Thread Name: {threadName}{newline}    Extended Properties: {dictionary({key} - {value}{newline})}"
            name="Detailed" />
        <add
            type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="{timestamp(FixedFormatTime)} {category} {message}{newline}"
            name="Short" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="Console" />
                <add name="Logfile" />
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events">
            <listeners>
                <add name="Logfile" />
                <add name="Console" />
            </listeners>
        </allEvents>
        <notProcessed switchValue="All" name="Unprocessed Category">
            <listeners>
                <add name="Console" />
                <add name="Logfile" />
            </listeners>
        </notProcessed>
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Logfile" />
                <add name="Console" />
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>

1 个答案:

答案 0 :(得分:0)

没关系。我终于找到了问题。我的控制台模式启动了实际代码,而在服务模式下,我的代码未被调用 - 因此没有记录:)。