如何使用程序集名称命名 log4net 输出文件?
是否有任何变量可以用来命名我的日志文件?
<file value="${ALLUSERSPROFILE}\${AssemblyName}.log.xml" />
的地方
${ALLUSERSPROFILE}
真正起作用${AssemblyName}
不起作用,这是我为了说明我想要的而编造的东西。
Is there any variable which I could use to name my log files?
<file value="${ALLUSERSPROFILE}\${AssemblyName}.log.xml" />
Where
${ALLUSERSPROFILE}
really works${AssemblyName}
doesn't, it is something I made up just for illustration of what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他人所说,程序集名称没有内置占位符,并且有几种方法可以实现它。另一种方法是向 Log4Net 框架注册您自己的处理程序/转换器。
基本上,您执行与 log4net.Util.PatternString< 相同的操作/a> 类在内部执行(您可以检查相关源代码以获得比下面给出的片段更完整的“示例”)。
示例:
然后代码:
然后提供匹配的“Converter Class”。
最后让 Log4Net 框架知道您的转换器:
As others said, there is no builtin placeholder for the assemblyname and a couple of ways to achieve it anyhow. Another one is to register your own handler/converter with the Log4Net framework.
Basically, you do the same that the log4net.Util.PatternString class does internally (and you might check the relevant source code for a more complete "example" than the fragements given below).
Example:
Then the code:
Then provide a matching "Converter Class".
And finally make your converter known to the Log4Net framework:
默认情况下不是。
此语法用于扩展Windows 环境变量。虽然
ALLUSERSPROFILE
是标准环境变量,但AssemblyName
不是。您必须自己设置AssemblyName
,这作为动态解决方案并不容易\不可能。Not by default.
This syntax is for expanding Windows Environment Variables. Whilst
ALLUSERSPROFILE
is a standard environment variable,AssemblyName
is not. You would have to setAssemblyName
yourself, which will not be easy\possible as a dynamic solution.NLog 中的 processname 类似于 log4net 中的 PatternLayout ;它们都是日志本身布局/渲染的一部分。
如果您想在应用程序中使用环境变量,可以在安装过程中强制创建环境变量,也可以在代码中创建自己的环境变量; 此处有一个 C# 示例。
如果该选项不适合您的需求,那么您可以动态设置路径,如下所示 这里。
processname within NLog is akin to the PatternLayout within log4net; they are both part of the layout/rendering of the logs themselves.
If you want to use an environment variable within your application either force the creation as part of the install or create your own within your code; a C# example is here.
If that option does not suit your needs then you can dynamically set the path as shown here.
您可以编写自己的图案布局转换器。我发布了一个示例 这里,我认为很容易修改它,以便它写入程序集名称......
You could write your own pattern layout converter. I posted a sample here and I think it is easy to modify that so that it writes the assembly name...
另一种选择是,在初始化时定义一个全局属性:
在您的配置文件中,您可以使用“pid”属性,如下所示:
希望,我很清楚!
br++马布拉
Another option is, to define a global property at initialization time:
In your configuration file, you can use the "pid" property like this:
Hope, I was clear!
br++mabra