如何使log4net消息的一部分成为大写

时间:2021-12-18 05:35:39

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

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

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

[主机名] - foo吧。

But i want the output like

但我希望输出像

[HOSTNAME] - foo bar.

[HOSTNAME] - foo吧。

How can i make the hostname uppercase using conversion patterns?

如何使用转换模式使主机名大写?

Regards,

Cankut

2 个解决方案

#1


The solution suggested by Ron Grabowski is extending PatternConverter.

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());
    }
}

usage in configuration file:

在配置文件中的用法:

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

#2


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.

FWIW(也许其他人会觉得这很有用),NLog 2.0(也许是1.0 Refresh)添加了一些布局“包装”,允许修改布局的输出。看到这个链接,向下。

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:

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

${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.

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

#1


The solution suggested by Ron Grabowski is extending PatternConverter.

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());
    }
}

usage in configuration file:

在配置文件中的用法:

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

#2


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.

FWIW(也许其他人会觉得这很有用),NLog 2.0(也许是1.0 Refresh)添加了一些布局“包装”,允许修改布局的输出。看到这个链接,向下。

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:

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

${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.

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