How To? Log4Net Wrapper And Get Log4Net Output File

11 במרץ 2009

תגיות: ,
תגובה אחת

 

image I am using Log4Net as a logging framework for a small application I am writing.

I needed to get the full path to the output file that Log4Net is generating. Apparently it is quite hidden…So here is a code that will help you get that file:

public static string LogFile
{
get
{
return ((FileAppender)LoggerService.Logger.Logger.Repository.GetAppenders()[0]).File;
}
}

The loggerService is my Log4Net wrapper which is fully tested and you can use it here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using System.IO;
using System.Windows.Forms;

namespace Core.Services
{
public static class LoggerService
{
public static ILog Logger
{
get;
private set;
}

public static string LoggerConfigFile
{
get;
private set;
}

public static bool IsInitialized
{
get;
private set;
}

public static void ShutdownService()
{
lock (typeof(LoggerService))
{
if (!IsInitialized)
{
throw new CoreException(Core.Res.Core.Core_Logger_ShutdownException);
}

IsInitialized = false;
Logger = null;
LoggerConfigFile = null;
LogManager.Shutdown();
}
}

public static void InitializeService()
{
lock (typeof(LoggerService))
{
if (IsInitialized)
{
throw new CoreException(Core.Res.Core.Core_Logger_InitializationException);
}

Logger = LogManager.GetLogger(typeof(LoggerService));
LoggerConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

if (!File.Exists(LoggerConfigFile))
{
throw new IOException(string.Format(Core.Res.Core.Core_FileSystem_FileNotExistFormat, LoggerConfigFile));
}

XmlConfigurator.ConfigureAndWatch(new FileInfo(LoggerConfigFile));
IsInitialized = true;
}
}

public static void Debug(object msg)
{
lock (typeof(LoggerService))
{
Logger.Debug(msg);
}
}

public static void DebugFormat(string format,params object[] parameters)
{
lock (typeof(LoggerService))
{
Logger.DebugFormat(format, parameters);
}
}

public static void Info(object msg)
{
lock (typeof(LoggerService))
{
Logger.Info(msg);
}
}

public static void InfoFormat(string format, params object[] parameters)
{
lock (typeof(LoggerService))
{
Logger.InfoFormat(format, parameters);
}
}

public static void Warn(object msg)
{
lock (typeof(LoggerService))
{
Logger.Warn(msg);
}
}

public static void Warn(object msg, Exception ex)
{
lock (typeof(LoggerService))
{
Logger.Warn(msg, ex);
}
}

public static void WarnFormat(string format, params object[] parameters)
{
lock (typeof(LoggerService))
{
Logger.WarnFormat(format, parameters);
}
}

public static void Error(object msg)
{
lock (typeof(LoggerService))
{
Logger.Error(msg);
}
}

public static void Error(object msg, Exception ex)
{
lock (typeof(LoggerService))
{
Logger.Error(msg, ex);
}
}

public static void ErrorFormat(string format, params object[] parameters)
{
lock (typeof(LoggerService))
{
Logger.ErrorFormat(format, parameters);
}
}

public static void Fatal(object msg)
{
lock (typeof(LoggerService))
{
Logger.Fatal(msg);
}
}

public static void Fatal(object msg, Exception ex)
{
lock (typeof(LoggerService))
{
Logger.Fatal(msg, ex);
}
}

public static void FatalFormat(string format, params object[] parameters)
{
lock (typeof(LoggerService))
{
Logger.FatalFormat(format, parameters);
}
}
}
}

I hope this helps.

kick it on DotNetKicks.com

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

תגובה אחת

  1. Amir14 במרץ 2009 ב 16:36

    If you are not using log4net v1.2.10 (the latest) it is recommended to add in your wrapper IsDebugEnabled (and the rest of the gang…) calls, otherwise you will spent useless time in GC after working for nothing.
    You should return ASAP if the level of logging is not enabled.
    This is an optimization from a performance perspective.

    Beside of that, it's a good practice to minimize expensive data formatting in the application if the level of logging is not enabled.

    להגיב