ELMAH error logging for Windows Service
-
18-09-2019 - |
Question
We are using ELMAH to log the web application exception which works great!. Also we want to know how to customize ELMAH to log the windows service exception.
I am not interested in using another application for logging only windows service exception.
Any help would be appreciated.
Solution
I did this previously. I found out the code to use by digging though the source code starting from the ErrorSignal.FromCurrentContext().Raise(ex); method.
This currently only logs to the Database (as thats all i needed) but with a little bit more investigation you could write a wrapper method that logs to whatever you have set-up in the config file.
try
{
Elmah.SqlErrorLog ErrorLog = new Elmah.SqlErrorLog(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
ErrorLog.ApplicationName = "AppName";
ErrorLog.Log(new Elmah.Error(new Exception("example")));
}
catch (Exception ex)
{
//catch sql error
}
In my service I made the ErrorLog variable a public singleton object that was easily accessed from the service project.
OTHER TIPS
you can use it
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Error text")));