I have an asp.net mvc 3 application. In this application I have a reminder system that uses quartz to grab messages from the database and send them out.
I am wondering what is the best way to log if something happens(say the database times out - I want to know about this).
I use in my mvc application ELMAH for my logging and it works great. However since quartz.net is it's own thread with out the httpcontext I can't use ELMAH(or at least I don't think I can). I tried to make an httpcontext by fi开发者_Go百科rst querying my home page then through the scheduler code and using that as the context but that did not work.
System.ArgumentNullException was unhandled by user code
Message=Value cannot be null.
Parameter name: application
Source=Elmah
ParamName=application
StackTrace:
at Elmah.ErrorSignal.Get(HttpApplication application)
at Elmah.ErrorSignal.FromContext(HttpContext context)
at Job.Execute(JobExecutionContext context) in Job.cs:line 19
at Quartz.Core.JobRunShell.Run()
InnerException:
ErrorSignal.FromCurrentContext().Raise(new System.Exception());
So I am looking for either how to get ELMAH working in this scenario or something comparable to it(something that sends emails, stack trace and everything nice ELMAH has).
You can try NLog. It's very simple to implement and effective.
You can send email, trace pretty much everything.
I normally tend to keep everything in a separate config file (NLog.Config)
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target name="DebugHandler" type="File" filename="${basedir}/_Logs/${date:format=yyyyMMdd}_${level}.Log"
layout="${longdate} ${logger} ${aspnet-session:variable=UserName} ${threadid} ${environment} ${identity} ${aspnet-request} ${message} ${exception}" />
<target name="ErrorHandler" type="File" filename="${basedir}/_Logs/${date:format=yyyyMMdd}_${level}.Log"
layout="${longdate} ${logger} ${aspnet-session:variable=UserName} ${threadid} ${environment} ${aspnet-request} ${message} ${exception}" />
<target name="FatalHandler" type="File" filename="${basedir}/_Logs/${date:format=yyyyMMdd}_${level}.Log"
layout="${longdate} ${logger} ${aspnet-session:variable=UserName} ${threadid} ${environment} ${aspnet-request} ${message} ${exception}" />
<target name="GenericHandler" type="File" filename="${basedir}/_Logs/${date:format=yyyyMMdd}_${level}.Log"
layout="${longdate} ${logger} ${aspnet-session:variable=UserName} ${threadid} ${environment} ${aspnet-request} ${message} ${exception}" />
</targets>
<rules>
<logger name="*" level="Debug" appendTo="DebugHandler" />
<logger name="*" level="Error" appendTo="ErrorHandler" />
<logger name="*" level="Fatal" appendTo="FatalHandler" />
<logger name="*" levels="Info,Warn" appendTo="GenericHandler" />
</rules>
</nlog>
As you can see you can activate or deactivate the different levels.
I've used it on in my quartz.net jobs as well as a debugging/tracing system and it does its job.
The only limit is you do not have an ELMAH interface, which can be a huge limit sometimes.
精彩评论