[转]Global exception handling in Web API 2.1 and NLog
本文轉自:https://stackoverflow.com/questions/25865610/global-exception-handling-in-web-api-2-1-and-nlog
?
In Web API 2.1 is new Global Error Handling.
I found some example how to log exceptions into Elmah ( elmah sample ).
But I use NLog to log errors into database table.
Is it posible to use Web API Global Error Handling with NLog?
Please provide some example.
?
?
It's actually quite simple, you either implement IExceptionLogger by hand or inherit from the base class, ExceptionLogger.
public class NLogExceptionLogger : ExceptionLogger {private static readonly Logger Nlog = LogManager.GetCurrentClassLogger();public override void Log(ExceptionLoggerContext context){Nlog.LogException(LogLevel.Error, RequestToString(context.Request), context.Exception);}private static string RequestToString(HttpRequestMessage request){var message = new StringBuilder();if (request.Method != null)message.Append(request.Method);if (request.RequestUri != null)message.Append(" ").Append(request.RequestUri);return message.ToString();} }Also add it to the config:
var config = new HttpConfiguration(); config.Services.Add(typeof(IExceptionLogger), new NLogExceptionLogger());You can find full sample solution here.
?
轉載于:https://www.cnblogs.com/freeliver54/p/8072485.html
總結
以上是生活随笔為你收集整理的[转]Global exception handling in Web API 2.1 and NLog的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谁想要安卓手机的苹果字体?
- 下一篇: 安卓智能手机使用字体大师替换系统字体教程