2

我有以下配置和代码,writer.logentry没有抛出异常但没有创建文件。

更新 1:我更改了侦听器,删除了事件日志并添加了平面文件日志。我仍然看不到创建的 .log 文件

我想我在配置中遗漏了一些东西?

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <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="AskAndTrack.log" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, Callstack" />
    </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>

    catch(Exception ex)
    {
        var logEntry = new LogEntry();
        logEntry.Message = ex.Message;
        Logger.Write(logEntry);
    }
4

2 回答 2

3

发布的配置看起来不错。如果发生错误记录,Enterprise Library 不会抛出异常——它将尝试将失败记录到错误特殊源(如果已配置)。您尚未为错误添加跟踪侦听器,但我建议您这样做。

您的问题看起来可能是权限。您能否验证该进程是否有权创建名为 的文件AskAndTrack.log?或者可能提前创建文件或写入明确授予权限的文件夹(例如,Everyone Full Control(仅用于测试!))。

于 2012-05-09T22:26:05.420 回答
1

在我看来,您缺少类别和优先级,这是我的片段。忽略辅助方法,它们只返回一个类别,例如您的“General”和一个优先级,例如“Error”。

                if (!Logger.Writer.IsLoggingEnabled()) return;

            var logEntry = new LogEntry { Severity = GetTraceEventTypeFromPriority(severity) };
            logEntry.Categories.Add(GetLogCategory(app_name, severity)); // CHANGED TO NONE BECAUSE SITECORE SUCKS
            logEntry.Priority = (int)severity;

            if (!Logger.ShouldLog(logEntry)) return;

            logEntry.Message = message;
            logEntry.Title = GetLogTitle(RequestDataManager.RequestData.blah, message, severity);

            lock (_locker)
            {
                Logger.Write(logEntry);
            }
于 2012-05-09T15:15:28.890 回答