Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net how to use both the ErrorFormat and the Error at the same time

Tags:

c#

log4net

In log4net there are 2 very nice functions:

Log.Error("Message", myException);

and

Log.ErrorFormat("Message {0}", CustomerId);

The produce log records in the output log file very nicely. The exception one is nice because it prints a stack trace and all the exception details nicely, and the Format one is nice because it allows me to specifiy the parameters that caused the failure. Is there a "best of both worlds" method, or some way to make it?

Log.ErrorFormatEx("Message {0}", CustomerId, myException)

Edit: I went through the possible duplicate questions, but they are badly formatted and don't ask it exactly like this.. I was looking for an extension method or existing method

like image 454
user230910 Avatar asked May 07 '15 06:05

user230910


2 Answers

Know this has been answered already, but just for other users who might find this alternative useful. I created a ILog Interface and a Log Class to "centralize" my log4net methods and logic. Also I created multiple overloads for the "Error" method.

ILog.cs

public interface ILog
{
    void Error(Exception exception);
    void Error(string customMessage, Exception exception);
    void Error(string format, Exception exception, params object[] args);
    void Warn(Exception exception);
    void Info(string message);
}

Log.cs

public class Log : ILog
{
    public void Error(Exception exception)
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(exception.TargetSite.DeclaringType);
        logger.Error(exception.GetBaseException().Message, exception);
    }

    public void Error(string customMessage, Exception exception)
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(exception.TargetSite.DeclaringType);
        logger.Error(customMessage, exception);
    }

    public void Error(string format, Exception exception, params object[] args)
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(exception.TargetSite.DeclaringType);
        logger.Error(string.Format(format, args), exception);
    }

    public void Warn(Exception exception)
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(exception.TargetSite.DeclaringType);
        logger.Warn(exception.GetBaseException().Message, exception);
    }

    public void Info(string message)
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        logger.Info(message);
    }
}

Example usage

public MyClass DeserializeJsonFile(string path)
{
    try
    {
        using (StreamReader r = new StreamReader(path))
        {
            string json = r.ReadToEnd();
            return JsonConvert.DeserializeObject<MyClass>(json);
        }
    }
    catch (Exception ex)
    {       
        this.log.Error("Error deserializing  jsonfile. FilePath: {0}", ex, path);
        return null;                
    }
}
like image 34
jpgrassi Avatar answered Sep 21 '22 23:09

jpgrassi


You could create an extension method:

namespace log4net.Core
{
    public class Log4NetExtensions 
    {    
         public static void ErrorFormatEx(this ILog logger, string format, Exception exception, params object[] args) 
         {
               logger.Error(string.Format(format, args), exception);
         }
    }
}

Then you can use it just like you would use any other Log4Net method:

Log.ErrorFormatEx("Message {0}", exception, CustomerId);
like image 179
Loetn Avatar answered Sep 19 '22 23:09

Loetn