How to log text message in case of successful web requests using ELMAH and C#

StackOverflow https://stackoverflow.com/questions/17699876

  •  03-06-2022
  •  | 
  •  

Вопрос

Can anyone help me to know regarding how to log custom message using ELMAH and C# ,in case of successful web requests return status code 200.

Is there any method similar to the one as mentioned below :

Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

to log custom message.

A code sample would be very useful.

Thanks & Regards, Santosh Kumar Patro

Это было полезно?

Решение

As far as I know, ELMAH only handles exceptions (Error Logging Modules and Handlers), but with this little trick you can make it log whatever you want, whenever you want

Note that this may not be the only or best approach to solve your problem

ErrorSignal.FromCurrentContext().Raise(new NotImplementedException("blah blah blah whatever you want")); //ELMAH Signaling

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top