Search code examples
c#log4net-configuration

Log4Net regional/cultural settings


How does one configure log4nets regional / culture info settings?

If I pass a date to the format methods it gets logged in US format, ignoring my application and o/s settings

log.InfoFormat("Invalid date of {0} found in field {1}", date, field.Name);

This writes out the date in month / date / year format which doesn't match my current culture into of date / month / year.

Am I missing something really simple?


Solution

  • Internally in log4net's default ILog implementation all of the calls of InfoFormat, WarnFormat, DebugFormat, etc. use CultureInfo.InvariantCulture if it was not specified explicitly (the methods have overloads). Which is kinda stupid since something like Thread.CurrentThread.CurrentCulture would have been more correct, but what can we do.

    So... You can specify your IFormatProvider of choice explicitly to have it used there. For example

    log.InfoFormat(CultureInfo.CurrentCulture, "Invalid date of {0} found in field {1}", date, field.Name);
    

    Or, alternatively, You can have this proxied in your own logger class that calls log4net logger object internally with the culture:

    public static class Logger
    {
        ...
        public static void InfoFormat(string pattern, params object[] values)
        {
            _log4net.InfoFormat(CultureInfo.CurrentCulture, pattern, values);
        }