[转]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.
[转]Global exception handling in Web API 2.1 and NLog的更多相关文章
- Global exception handling in asp.net core webapi
在.NET Core中MVC和WebAPI已经组合在一起,都继承了Controller,但是在处理错误时,就很不一样,MVC返回错误页面给浏览器,WebAPI返回Json或XML,而不是HTML.Us ...
- Custom Exception in ASP.NET Web API 2 with Custom HttpResponse Message
A benefit of using ASP.NET Web API is that it can be consumed by any client with the capability of m ...
- [翻译]ASP.NET Web API 2 中的全局错误处理
目录 已存在的选项 解决方案预览 设计原则 什么时候去用 方案详情 示例 附录: 基类详情 原文链接 Global Error Handling in ASP.NET Web API 2 由于翻译水平 ...
- Model Validation in ASP.NET Web API By Mike Wasson|July 20, 2012 268 of 294 people found this helpful
using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using ...
- ErrorHandling in asp.net web api
https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling https://d ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...
- Global Error Handling in ASP.NET Web API 2(webapi2 中的全局异常处理)
目前,在Web API中没有简单的方法来记录或处理全局异常(webapi1中).一些未处理的异常可以通过exception filters进行处理,但是有许多情况exception filters无法 ...
- NSwag在asp.net web api中的使用,基于Global.asax
https://github.com/NSwag/NSwag/wiki/OwinGlobalAsax This page explains how to use the NSwag OWIN midd ...
随机推荐
- python 几种方法实现随机生成8位同时包含数字、大写字符、小写字符密码的小程序
python 实现随机生成包8位包含大写字母.小写字母和数字的密码的程序.要求:1用户输入多少次就生成多少条密码,2要求密码必须同时包含大写字母.小写字母和数字,长度8位,不能重复代码如下: impo ...
- 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构
本篇承接上篇内容,如果你不小心点击进来,建议重新从第一篇开始完整阅读. 构建NetCore应用框架之实战篇索引 一.BitAdminCore框架简介 从前篇论述我们知道,我们接下来将要去做一个管理系统 ...
- WinForm中的图表控件Chart
第一次接触Chart控件,发现了这个Chart控件的实例项目,非常强大,用示例的方法介绍了该控件各式各样的用法. 下载链接
- 无废话网页重构系列——(3)Web重构前的分析
本篇讲重构前的分析.从“工作状态.工作环境和工作角色”和具体重构工作两方面分析. 凡是经过考验的朋友,就应该把他们紧紧地团结在你的周围 比较理想的工作状态:制定了各种设计和开发规范,各团队之间邮件.团 ...
- 枚举类型内部函数 enumerate
enumerate()说明enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumera ...
- dfs找負環
某些無良出題人可能會卡bfs找負環,所以要用dfs 核心代碼(以jzoj5173為例): #include<bits/stdc++.h> using namespace std; #def ...
- poj1017----模拟
题目大意: 现有1*1,2*2,3*3,4*4,5*5,6*6规格的产品若干个(高度都为h),问最少需要多少个 6*6*h的箱子把这些产品都装完 输入:每组测试数据共6个整数,分别代表1*1,...6 ...
- cassandra用户名和密码的设置
设置Cassandra使用用户名和密码验证的步骤如下: 1.修改${CASSANDRA_HOME}/conf/cassandra.yaml,把authenticator: AllowAllAuthen ...
- centos6.5 yum安装lamp
准备篇: 1.清空防火墙 iptables -F 或者关闭防火墙 /etc/init.d/iptables stop,如果要防火墙开机不要启动 chkconfig iptables off 2.关闭S ...
- HTML5技术要点
HTML5技术要点 1.HTML5视频 <!DOCTYPE HTML> <html> <body> <video src="/i/movie.ogg ...