如何将 log4net 消息的一部分变为大写

发布于 2024-07-25 14:33:20 字数 604 浏览 4 评论 0原文

我正在使用 AdoNetAppender 来记录消息。 我已将 %property{log4net:HostName} 转换模式添加到消息参数中。

<parameter>
      <parameterName value="@message"/>
      <dbType value="String"/>
      <size value="4000"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%property{log4net:HostName}] - %message"/>
      </layout>
</parameter>

输出类似于

[主机名] - foo bar。

这样的输出

但我想要像[HOSTNAME] - foo bar

。 如何使用转换模式将主机名变为大写?

问候,

坎库特

I'm using AdoNetAppender to log messages. I've added %property{log4net:HostName} conversion pattern to the message parameter.

<parameter>
      <parameterName value="@message"/>
      <dbType value="String"/>
      <size value="4000"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%property{log4net:HostName}] - %message"/>
      </layout>
</parameter>

Output is like

[hostname] - foo bar.

But i want the output like

[HOSTNAME] - foo bar.

How can i make the hostname uppercase using conversion patterns?

Regards,

Cankut

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

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

发布评论

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

评论(2

梦里泪两行 2024-08-01 14:33:21

Ron Grabowski 建议的解决方案是扩展 PatternConverter。

public class HostNameToUpperConverter : PatternConverter
{
    protected override void Convert(TextWriter writer, object state)
    {
        string hostName = (string)GlobalContext.Properties[LoggingEvent.HostNameProperty];
        writer.Write(hostName.ToUpper());
    }
}

配置文件中的用法:

<layout type="log4net.Layout.PatternLayout">
        <converter>
          <name value="hostNameToUpper" />
          <type value="MyApplication.HostNameToUpperConverter" />
        </converter>
        <conversionPattern value="[%hostNameToUpper] - %message" />
</layout>

The solution suggested by Ron Grabowski is extending PatternConverter.

public class HostNameToUpperConverter : PatternConverter
{
    protected override void Convert(TextWriter writer, object state)
    {
        string hostName = (string)GlobalContext.Properties[LoggingEvent.HostNameProperty];
        writer.Write(hostName.ToUpper());
    }
}

usage in configuration file:

<layout type="log4net.Layout.PatternLayout">
        <converter>
          <name value="hostNameToUpper" />
          <type value="MyApplication.HostNameToUpperConverter" />
        </converter>
        <conversionPattern value="[%hostNameToUpper] - %message" />
</layout>
红尘作伴 2024-08-01 14:33:21

FWIW(也许其他人会发现这很有用),NLog 2.0(也许还有 1.0 刷新)添加了一些布局允许修改布局输出的“包装器”。 请参阅底部的链接

包装器包括大写、小写、修剪空白、填充等等。 因此,您可能可以定义一个布局,指定要包含在输出中的字段,并将整个内容或部分内容“包装”在“大写”包装器中。 我真的不知道确切的语法,但我知道您可以在其他布局中包含布局,这样您就可以为“主机名”定义布局,将其包装在“大写”包装器中,然后将该布局包含在最终布局中。 在伪代码中(配置文件伪代码,而不是代码伪代码)(非常伪!):

var host=${gdc:hostname}
var uhost=${uppercase=true,inner=host}

现在 uhost 可以包含在“真实”布局中:

${datetime} | ${uhost} | ${message}

或者,您可以这样做:

${datetime} | ${uppercase=true, inner=${gdc:hostname}} | ${message}

请注意,我实际上并没有尝试在 NLog 中执行此操作,我只是查看他们网站上的内容。

FWIW (maybe someone else will find this useful), NLog 2.0 (and maybe the 1.0 Refresh) has added some layout "wrappers" that allow modification of the output of a layout. See this link, towards the bottom.

Among the wrappers are uppercase, lowercase, trim whitespace, pad, and a few more. So, you could probably define a layout that specifies the fields to be included in the output and "wrap" either whole thing or part of it in the "uppercase" wrapper. I don't really know the exact syntax I do know that you can include layouts in other layouts so you could define a layout for "host name", wrap it in the "uppercase" wrapper, and then include that layout in the final layout. In pseudocode (configuration file pseudocode, not code pseudocode) (very pseudo!):

var host=${gdc:hostname}
var uhost=${uppercase=true,inner=host}

Now uhost can be included in the "real" layout:

${datetime} | ${uhost} | ${message}

Or, you might do it like this:

${datetime} | ${uppercase=true, inner=${gdc:hostname}} | ${message}

Note that I have not actually tried doing this in NLog, I am just going by what is on their website.

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