Send ELMAH output via NLog

I am currently using NLog to log security events from my ASP.NET MVC3 application.

Now we have one more requirement to add application errors to a separate log.

Since we already use NLog (and love the flexibility of targets), I would like to configure Elmah to send errors to NLog for logging.

Has anyone done this and shared this?

+5
source share
1 answer

The same query is written for Log4Net on the blog

I translated it into NLog and updated it to an NLog recommendation:

public class NLogErrorLog : XmlFileErrorLog
{
    private static Logger logger = LogManager.GetLogger("elmah");

    public NLogErrorLog(IDictionary config) : base(config)
    {
    }


    public NLogErrorLog(string logPath) : base(logPath)
    {
    }

    public override string Log(Error error)
    {
        //Write whatever you want to NLog
        logger.Error(error.Exception, "Exception logged through ELMAH: " + error.Message);
        return base.Log(error);
    }
}

register in web.config

<elmah>  
    <errorLog type="MyAssembly.NLogErrorLog, MyAssembly" logPath="~/App_Data" />  
</elmah>
0
source

All Articles