DCSIMG

 Subscribe in a reader

How To? Log4Net Wrapper And Get Log4Net Output File - Guy kolbis
Wednesday, March 11, 2009 7:14 PM kolbis

How To? Log4Net Wrapper And Get Log4Net Output File

 

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 תגים:,

תוכן התגובה

# Log4Net Wrapper

DotNetKicks.com כתב/ה

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, March 11, 2009 7:24 PM

# re: How To? Log4Net Wrapper And Get Log4Net Output File

Amir כתב/ה

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.

Saturday, March 14, 2009 4:36 PM

שלח תגובה

(שדה חובה) 
(שדה חובה) 
(אופציונלי)
(שדה חובה) 

Enter the numbers above: