简体   繁体   English

记录应用程序块未记录到文件

[英]Logging Application Block not logging to a file

I have the following configuration and code, the writer.logentry is not throwing an exception but the file is not created. 我具有以下配置和代码, writer.logentry不会引发异常,但不会创建文件。

Update 1: I changed the listener, I removed eventlog and added flat file log. 更新1:我更改了侦听器,删除了事件日志并添加了平面文件日志。 I still cant see a created .log file 我仍然看不到已创建的.log文件

I suppose I am missing something in the configuration? 我想我在配置中缺少什么?

<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);
    }

The posted configuration looks OK. 发布的配置看起来不错。 Enterprise Library will not throw an exception if an error occurs logging -- it will attempt to log the failure to the errors special source (if configured). 如果在记录错误时发生错误,企业库将不会引发异常-它会尝试将失败记录到错误特殊来源(如果已配置)。 You haven't added a Trace Listener for errors but I recommend doing so. 您尚未添加跟踪侦听器来查找错误,但我建议这样做。

Your issue looks like it is probably permissions. 您的问题看起来可能是权限。 Can you verify that the process has permissions to create a file called AskAndTrack.log ? 您可以验证该进程是否有权创建名为AskAndTrack.log的文件吗? Or perhaps create the file ahead of time or write to a folder where the permissions are explicitly granted (eg Everyone Full Control (for testing only!)). 或者,可能提前创建文件或写入明确授予了权限的文件夹(例如,“所有人完全控制”(仅用于测试!))。

Look to me like you are missing the category and priority, here is my snippet. 在我看来,您缺少类别和优先级,这是我的摘录。 Ignore the helper methods they just return a category such as your 'General' and a priority such as '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);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM