c# - nLog 丢失日志数据

标签 c# logging nlog

有时 nLog 不会记录所有调试信息(调试文件中缺少一些 log.Debug...),有谁知道为什么会发生这种情况以及如何避免这种情况?

这是我的 nLog 配置

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <variable name="appTitle" value="Service"/>
  <targets async="true">
    <target name="fallbackDebug" xsi:type="FallbackGroup" returnToFirstOnSuccess="true">
      <target xsi:type="File" fileName="C:\Logs\${date:format=yyyyMMdd}_Debug.txt" layout="..."/>
      <target xsi:type="File" fileName="C:\Logs\${date:format=yyyyMMdd}_Debug_bu.txt" layout="..."/>
    </target>
  </targets>
  <rules>
    <logger name="*" levels="Trace,Debug,Error,Fatal"       writeTo="fallbackDebug" />
  </rules>
</nlog>

在代码中我使用以下内容

private static Logger log = LogManager.GetCurrentClassLogger();
...
log.Debug("Some debug info");

谢谢!

最佳答案

您必须在最后调用 LogManager.Flush() 方法。

示例:

private static Logger log = LogManager.GetCurrentClassLogger();
...
log.Debug("Some debug info");
...
LogManager.Flush();

关于c# - nLog 丢失日志数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806459/

相关文章:

c# - NLog 找不到或加载配置文件

.net - 使用 NLog 记录异常时如何获取堆栈跟踪?

c# - 如何禁用 .NET/GDI+ 的子采样?

c# - 在基类中定义的 log4net 记录器

asp.net - 数据注释用于在记录之前清理请求和响应

.net - Windows 窗体的未处理异常日志记录

c# - NLog 自定义日志级别值

c# - RestSharp 在 POST 上默认 Content-Type 为 application/x-www-form-urlencoded

c# - SocketAsyncEventArgs 加密——SSL 与自定义预传输加密

c# - Windows 窗体卡在多个异步任务上