10

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.

mtk
  • 13,221
  • 16
  • 72
  • 112
Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154

6 Answers6

13
%property{log4net:HostName}
JC.
  • 11,561
  • 11
  • 41
  • 50
12

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}

Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
3

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/

Adauto
  • 569
  • 3
  • 5
2

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
AgileJon
  • 53,070
  • 5
  • 41
  • 38
2

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.

Community
  • 1
  • 1
Randall311
  • 31
  • 3
0

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

Storm
  • 4,307
  • 11
  • 40
  • 57