我很难找到有关各种“框内”模式的文档,例如
%logger
%level
%timestamp
当然有 示例页面,但我不确定这是否是完整的选项列表。
我还知道可以将 MDC 参数从应用程序传输到记录器,但这涉及代码更改,这与配置更改不同。
是否有 %machineName
选项或 machineIP
选项? 问题在于,我们将网络场中的所有服务器都记录到同一数据库日志中,并且我们现在认为来自一台计算机的消息数量不成比例。
I'm having a hard time finding documentation on the various 'in the box' patterns like
%logger
%level
%timestamp
There is of course the example page but I'm not sure that's the full list of options.
I also know that it's possible to MDC parameters out of the app to the logger, but that involves a code change which is a different beast than a config change.
Is there a %machineName
option, or machineIP
option? The issue is that we have all our servers in the web farm log into the same database log, and we're now thinking that a disproportionate number of messages are coming from one machine.
发布评论
评论(6)
使用这个关于添加 GlobalContext 属性的答案https://stackoverflow.com/a/2096452/1224858,我能够得到这个工作。
我在我的类文件中添加了以下代码:
然后在配置文件中我可以引用主机名,它会出现
希望这有帮助。
Using this answer https://stackoverflow.com/a/2096452/1224858 about adding GlobalContext properties, I was able to get this to work.
I added the following code in my class file:
And then in the config file I can reference hostname and it will appear
Hope this helps.
查看 PatternString API,看起来您需要在模式中使用 %property。 另请查看 本文,您可能需要在应用程序启动时将计算机名称注入到全局上下文中。
Check out the PatternString API, it looks like you would need to use %property in your pattern. Also take a look at this article, you may need to inject the machine name into the global context on application startup.
有趣的是,我认为这是“紧凑参数语法”,请查看这里的最后一部分 http://logging.apache.org/log4net/release/manual/configuration.html
Interesting, I think this is the "Compact Parameter Syntax" check out the last section over here http://logging.apache.org/log4net/release/manual/configuration.html
我偶然发现的事情
似乎是有效的——想知道这个和建议的其他选项之间有什么区别。 就像
%property{log4net:HostName}
What I did just stumble across is
and that seems to be working --- wonder what the difference is between this and the other options suggested. like
%property{log4net:HostName}
创建一个获取机器名称的类:
然后像这样设置 log4net 配置:
我喜欢这种方法,因为它可以重用,并且我可以按照我想要的方式管理信息。 例如,如果您想记录 IP 地址,只需执行相同操作并创建转换器,如下所示:
有关链接的更多信息: http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter- for-log4net/
Create a class that gets the machine name:
then set your log4net configuration like this:
I like this approach as it can be reused and i can manage the information how i want. If you want to log the ip address for example, just do the same and create the converter like so:
More info on the link: http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter-for-log4net/