NLog OnNewLogFile 处理程序? [英] NLog OnNewLogFile Handler?

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

问题描述

有没有我可以挂钩的事件处理程序或类似的东西,以便在每次 NLog 启动新的日志文件时运行一些代码?

Is there any event handler or similar that I can hook into, to run some code every time NLog starts a new Log File?

我有一个这样的 NLog 配置:

I have an NLog Config like this:

    <target name="logfile" xsi:type="File"
            fileName="[stuff]"
            layout="[more Stuff]"
            maxArchiveFiles="4"
            archiveAboveSize="102400"  />

而且我有点想在每个日志文件的开头添加一些元数据(版本号、机器名称等,以便每个日志文件都包含一些上下文信息)

And I'd kinda like to add some meta-data at the start of every log file (version number, Machine Name, etc. so that every log file comes complete with some contextual information)

我不希望能够在 NLog 本身中配置此文本,但我希望我可以告诉 NLog 在每次要创建新的日志文件时运行某个方法.

I don't expect to be able to configure this text in NLog itself, but I'm hoping that I can tell NLog to run a certain method everytime it's going to create a new Log File.

这是一回事吗?

推荐答案

听起来您真的是在寻找 FileTarget 上的 Header-Layout.

Sounds like you are really looking for the Header-Layout on the FileTarget.

<target name="logfile" xsi:type="File">
         <header>--- ${assembly-version} started on ${longdate} ---</header>
</target>

另见 https://github.com/NLog/NLog/wiki/File-目标

有些将它与 archiveOldFileOnStartup="true" 结合使用,因为仅在创建文件时才会打印标题(确保应用程序重新启动将创建新文件).

Some combine it with archiveOldFileOnStartup="true" because the Header is only printed when file is created (ensure application restarts will create new file).

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

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