问题描述
我有 asp.net core 2.1
应用程序以及 HangFire 1.6.17
。 HangFire配置为以一定间隔执行后台作业。后台作业使用HttpClient调用外部API。如果http调用失败,则该方法将引发带有元数据的自定义异常。想法是hangfire将使用元数据记录异常。我遵循了创建异常
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我们像这样
如何在.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()方法被称为
,但是,我看不到Hangfire记录的任何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.
推荐答案
您在仪表板中看到的堆栈跟踪已格式化。您可以看到和。
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不使用元数据记录自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!