I realize this rather late to the party, but I came across this article wanting to post some "non-exception" information with Elmah as well.
I started with creating a new Exception
object with a custom name and that name would be in the Type
field in my database but that was not exactly what I wanted. I then noticed that Elmah wants an Error
object. And that the Type
field was { get; set; }
so I had my answer.
I created a static helper class to make it less code each time I needed it:
public static class ElmahTools {
public static Error LogHangFireInfo(string message) {
var Result = new Error(new Exception(message));
Result.Type = "Hangfire Info";
return Result;
}
}
At the top of the Class where I want to log information, I added the following:
private static readonly Elmah.ErrorLog ErrorRepo = Elmah.ErrorLog.GetDefault(null);
Then in my code, where I want to actually log info, I have the following:
ErrorRepo.Log(ElmahTools.LogHangFireInfo($"Job: {context.BackgroundJob?.Id} state has changed from {context.OldStateName} to {context.NewState.Name}"));
I initially was just going to do:
new Error { Type = "My Custom Info", Message = "Some message" };
But that failed because the TimeUtc
was not filled in. So I simply just create a rather empty Error
with the message and change just what I need to after (e.g. Type
.)
Hope this helps someone.