gpt4 book ai didi

c# - ASP.NET 网络 API 2 : ExceptionLogger and Exception Handler

转载 作者:行者123 更新时间:2023-11-30 16:00:47 27 4
gpt4 key购买 nike

我正在尝试在 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);
}
}

关于c# - ASP.NET 网络 API 2 : ExceptionLogger and Exception Handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com