我正在尝试在 Web api 中实现全局异常日志记录,并向用户发送一条带有该错误 ID 的友好消息,这样他就可以带着错误 ID 回到我们这里,以便我们可以修复它。我正在实施两者:
- System.Web.Http.ExceptionHandling.ExceptionLogger
- System.Web.Http.ExceptionHandling.ExceptionHandler
这是我的类,它重写了 ExceptionLogger 抽象类:
public class GlobalExceptionLogger : System.Web.Http.ExceptionHandling.ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
LogMessage logMsg = new LogMessage();
logMsg.ID = System.Guid.NewGuid().ToString();
logMsg.MessageType = MessageType.ResourceServerAPI;
logMsg.SenderMethod = context.Request.RequestUri != null ? string.Format("Request URL: {0}", context.Request.RequestUri.ToString()) : "";
logMsg.Level = MesageLevel.Error;
logMsg.MachineName = Environment.MachineName;
logMsg.Message = context.Exception.Message;
Logger.LogError(logMsg);
}
}
这是我处理错误的方式:
public class GlobalExceptionHandler : System.Web.Http.ExceptionHandling.ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
var metadata = new ErrorData
{
Message = "An error occurred! Please use the ticket ID to contact our support",
DateTime = DateTime.Now,
RequestUri = context.Request.RequestUri,
ErrorId = //get the ID already logged
};
var response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, metadata);
context.Result = new ResponseMessageResult(response);
}
}
由于异常日志记录发生在处理之前,因此将 ID 从异常记录器传递到异常处理程序以向最终用户发送相应的错误 ID 的最佳方法是什么?
你可以将它添加到HttpRequestMessage.Properties
:
public static class HttpRequestMessageExtensions
{
private const string LogId = "LOG_ID";
public static void SetLogId(this HttpRequestMessage request, Guid id)
{
request.Properties[LogId] = id;
}
public static Guid GetLogId(this HttpRequestMessage request)
{
object value;
if (request.Properties.TryGetValue(LogId, out value))
{
return (Guid) value;
}
return Guid.Empty;
}
}
然后在您的记录器/处理程序中使用这些扩展方法:
public class GlobalExceptionLogger : System.Web.Http.ExceptionHandling.ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
LogMessage logMsg = new LogMessage();
logMsg.ID = System.Guid.NewGuid().ToString();
logMsg.MessageType = MessageType.ResourceServerAPI;
logMsg.SenderMethod = context.Request.RequestUri != null ? string.Format("Request URL: {0}", context.Request.RequestUri.ToString()) : "";
logMsg.Level = MesageLevel.Error;
logMsg.MachineName = Environment.MachineName;
logMsg.Message = context.Exception.Message;
// Set the ID
context.Request.SetLogId(logMsg.ID);
Logger.LogError(logMsg);
}
}
public class GlobalExceptionHandler : System.Web.Http.ExceptionHandling.ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
// Get the ID
var id = context.Request.GetLogId();
var metadata = new ErrorData
{
Message = "An error occurred! Please use the ticket ID to contact our support",
DateTime = DateTime.Now,
RequestUri = context.Request.RequestUri,
ErrorId = id
};
var response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, metadata);
context.Result = new ResponseMessageResult(response);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)