Hanfire 未使用元数据记录自定义异常 [英] Hanfire not logging custom exception with metadata
问题描述
我有 asp.net core 2.1
应用程序以及 HangFire 1.6.17
.HangFire 被配置为以一定的时间间隔执行后台作业.后台作业使用 HttpClient 调用外部 API.如果 http 调用失败,则该方法会抛出带有元数据的自定义异常.想法是 hangfire 将记录带有元数据的异常.我遵循了 best-practices-for-exceptions 创建异常
I have asp.net core 2.1
application along with HangFire 1.6.17
. HangFire is configured to execute a background job at certain interval. The background job calls external API using HttpClient. If the http call fails, then the method throws custom exception with metadata. Idea is hangfire will log the exception with metadata. I followed best-practices-for-exceptions to create exception
public class MyHttpRequestException : Exception
{
public string Content { get; private set; }
public string RequestUri { get; private set; }
public string HttpResponse { get; private set; }
public MyHttpRequestException()
{
}
public MyHttpRequestException(string message)
: base(message)
{
}
public MyHttpRequestException(string message, Exception innerException)
: base(message, innerException)
{
}
public MyHttpRequestException(string message, string content, string httpResponse, string requestUri)
: base(message)
{
Content = content;
RequestUri = requestUri;
HttpResponse = httpResponse;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(base.ToString());
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("Content");
sb.AppendLine(Content);
sb.AppendLine("RequestUri");
sb.AppendLine(RequestUri);
sb.AppendLine("HttpResponse");
sb.AppendLine(this.HttpResponse);
return sb.ToString();
}
}
我也有 HttpResponseMessage
的扩展方法,它确保 API 请求成功,否则抛出 MyHttpRequestException
I also have extension method for HttpResponseMessage
which ensures API request is successful, and if not throws MyHttpRequestException
public static class HttpResponseMessageExtensions
{
public static async Task EnsureSuccessStatusCodeAsync(this HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return;
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var httpResponse = response.ToString();
var requestUri = response.RequestMessage.RequestUri.ToString()
if (response.Content != null)
response.Content.Dispose();
throw new MyHttpRequestException("Error while making http request.", content, httpResponse, requestUri);
}
}
这是我的后台作业,由 Hangfire 循环作业调度程序调用
Here is my background Job which is invoked by Hangfire recurring job scheduler
public async Task DoSomething(string url)
{
var response = await _httpClient.GetAsync(url)
await response.EnsureSuccessStatusCodeAsync();
// do something here if everything is okay
}
问题
当 EnsureSuccessStatusCodeAsync
方法抛出 MyHttpRequestException
时,Hangfire 会按预期记录异常,我在 HangFire 的仪表板中看到了这一点.但是 Hangfire 只记录异常消息和堆栈跟踪.我没有看到正在记录我的自定义属性(即 Content、RequestUri、HttpResponse)
Issue
When EnsureSuccessStatusCodeAsync
method throws MyHttpRequestException
then Hangfire logs the exception as expected, and i see that in HangFire's dashboard. However Hangfire only logs Exception message and stack trace. I don't see my custom properties are being logged ( ie. Content, RequestUri, HttpResponse)
在 clssic .NET 中,我们使用这样的 SerializationInfo SO post>
如何在 .NET Core 中创建自定义异常以便元数据也将被记录?
In clssic .NET we use SerializationInfo like this SO post
How do i create a custom exception in .NET Core so metadata will also get logged?
注意:
当 MyHttpRequestException
被抛出时,我注意到异常的 ToString() 方法被调用但是,我没有看到任何 ToString() 返回的内容被 Hangfire 记录下来.我不知道这是不是挂火问题,或者我需要以不同的方式实现 MyHttpRequestException.
Note:
When the MyHttpRequestException
gets thrown i noticed exception's ToString() method is getting called
however, i dont see whatever ToString() returns is getting logged by Hangfire.
I dont know if this is hangfire issue, or i need to implement MyHttpRequestException is different way.
推荐答案
您在 Dashboard 中看到的堆栈跟踪已格式化.你可以看到 noreferer>和
The stack trace that you see in Dashboard is formatted. You can see here and here.
由于这种自定义堆栈跟踪格式,您可以看到您的自定义属性.
Because this custom stack trace format you can see your custom properties.
这篇关于Hanfire 未使用元数据记录自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!