views:

178

answers:

2

Hello,

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

+1  A: 

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>
Cankut
A: 

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.

wageoghe