machineName 是否有 log4net 模式

发布于 2024-07-24 08:32:39 字数 402 浏览 3 评论 0 原文

我很难找到有关各种“框内”模式的文档,例如

%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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可可 2024-07-31 08:32:40

使用这个关于添加 GlobalContext 属性的答案https://stackoverflow.com/a/2096452/1224858,我能够得到这个工作。

我在我的类文件中添加了以下代码:

log4net.GlobalContext.Properties["hostname"] = Environment.MachineName;

然后在配置文件中我可以引用主机名,它会出现

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date [%thread] %-5level %logger [%property{hostname}] - %message%newline" />
</layout>

希望这有帮助。

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:

log4net.GlobalContext.Properties["hostname"] = Environment.MachineName;

And then in the config file I can reference hostname and it will appear

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date [%thread] %-5level %logger [%property{hostname}] - %message%newline" />
</layout>

Hope this helps.

巷子口的你 2024-07-31 08:32:40

查看 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.

允世 2024-07-31 08:32:40

有趣的是,我认为这是“紧凑参数语法”,请查看这里的最后一部分 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

念﹏祤嫣 2024-07-31 08:32:39
%property{log4net:HostName}
%property{log4net:HostName}
轻拂→两袖风尘 2024-07-31 08:32:39

我偶然发现的事情

<layout type="log4net.Layout.PatternLayout" value="${COMPUTERNAME}"/>

似乎是有效的——想知道这个和建议的其他选项之间有什么区别。 就像 %property{log4net:HostName}

What I did just stumble across is

<layout type="log4net.Layout.PatternLayout" value="${COMPUTERNAME}"/>

and that seems to be working --- wonder what the difference is between this and the other options suggested. like %property{log4net:HostName}

压抑⊿情绪 2024-07-31 08:32:39

创建一个获取机器名称的类:

using System;
using System.IO;
using log4net.Layout.Pattern;

namespace YourNameSpace.Converters
{
    public class MachinePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            writer.Write(Environment.MachineName);            
        }
    }
}

然后像这样设置 log4net 配置:

<layout type="log4net.Layout.PatternLayout">
    <converter>
      <name value="machine" />
      <type value="YourNameSpace.MachinePatternConverter" />
    </converter>
    <conversionPattern value="%date [%thread] %level %logger %machine" />
</layout>

我喜欢这种方法,因为它可以重用,并且我可以按照我想要的方式管理信息。 例如,如果您想记录 IP 地址,只需执行相同操作并创建转换器,如下所示:

public class IPPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
    {
        if (HttpContext.Current == null)
            return;

        writer.Write(HttpContext.Current.Request.UserHostAddress);
    }
}

有关链接的更多信息: http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter- for-log4net/

Create a class that gets the machine name:

using System;
using System.IO;
using log4net.Layout.Pattern;

namespace YourNameSpace.Converters
{
    public class MachinePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            writer.Write(Environment.MachineName);            
        }
    }
}

then set your log4net configuration like this:

<layout type="log4net.Layout.PatternLayout">
    <converter>
      <name value="machine" />
      <type value="YourNameSpace.MachinePatternConverter" />
    </converter>
    <conversionPattern value="%date [%thread] %level %logger %machine" />
</layout>

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:

public class IPPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
    {
        if (HttpContext.Current == null)
            return;

        writer.Write(HttpContext.Current.Request.UserHostAddress);
    }
}

More info on the link: http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter-for-log4net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文