每会话NLog [英] NLog per session

查看:62
本文介绍了每会话NLog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以配置NLog在每个应用程序会话中记录信息?到目前为止,每次执行应用程序(WinForm)时,它将消息添加到日志文件中.我们只想存储当前会话的信息.这意味着在启动应用程序时,将在记录任何新消息之前清除所有先前的消息.这样,日志文件中将仅提供当前会话的消息.

Is there any way to configure NLog to log information per application session? As of now it appends the messages in the log file each time application is executed (WinForm). What we'd like to have is to only store the info of the current session. Meaning that when application launches, all previous messages are cleared before any new message is logged. This way only the messages of current sessions will be available in the log file.

这是当前配置

 <?xml version="1.0"?>
<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="FileTarget" xsi:type="File" fileName="MainLogFile.txt" layout="${longdate} ${callsite} ${level} ${message}"/>/>
  </targets>
  <rules>
    <logger name="*" levels="Trace,Info,Warn,Error,Debug,Fatal" writeTo="FileTarget"/>
  </rules>
</nlog>

谢谢

推荐答案

假设您一次只能打开一个应用程序实例,则可以使用deleteOldFileOnStartup参数:

Assuming that you can only have one instance of your application open at once, you can just use the deleteOldFileOnStartup parameter:

  <targets>
    <target name="FileTarget" xsi:type="File" fileName="MainLogFile.txt" layout="${longdate} ${callsite} ${level} ${message}" deleteOldFileOnStartup="true">
  </targets>

这篇关于每会话NLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆