本文转自: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相关推荐

  1. Contact Manager Web API 示例[4] 异常处理(Exception Handling)

    联系人管理器web API是一个Asp.net web api示例程序,演示了通过ASP.NET Web API 公开联系信息,并允许您添加和删除联系人,示例地址http://code.msdn.mi ...

  2. 在ASP.NET Web API中返回错误的最佳实践

    本文翻译自:Best practice to return errors in ASP.NET Web API I have concerns on the way that we returns e ...

  3. 返璞归真 asp.net mvc (10) - asp.net mvc 4.0 新特性之 Web API

    返璞归真 asp.net mvc (10) - asp.net mvc 4.0 新特性之 Web API 原文:返璞归真 asp.net mvc (10) - asp.net mvc 4.0 新特性之 ...

  4. Web Api 基于Zookeeper的服务注册与发现

    差异 基于Nginx的服务提供和消费 基于zookeeper的服务注册和发现 zk的负载均衡是可以调控,nginx只是能调权重,其他需要可控的都需要自己写插件:但是nginx的吞吐量比zk大很多,可以 ...

  5. ASP.NET Web API之消息[拦截]处理(转)

    出处:http://www.cnblogs.com/Leo_wl/p/3238719.html 标题相当难取,内容也许和您想的不一样,而且网上已经有很多这方面的资料了,我不过是在实践过程中作下记录.废 ...

  6. ASP.NET Web API 应用教程(一) ——数据流使用

    相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web API 方面的技术,系列文章主要有三篇内容. 主要内容如下: I  数据 ...

  7. ASP.NET Core Web API 最佳实践指南

    原文地址: ASP.NET-Core-Web-API-Best-Practices-Guide 介绍 当我们编写一个项目的时候,我们的主要目标是使它能如期运行,并尽可能地满足所有用户需求. 但是,你难 ...

  8. WEB API 系列(二) Filter的使用以及执行顺序

    在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don't Repeat Yourself)思想 ...

  9. Asp.net web Api源码分析-HttpParameterBinding

    接着上文Asp.net web Api源码分析-Filter 我们提到filter的获取和调用,后面通过HttpActionBinding actionBinding = actionDescript ...

最新文章

  1. referenced from 异常
  2. POJ 2387 Til the Cows Come Home
  3. mysql二进制文件复制_MySQL 主从复制:基于二进制文件复制配置详解
  4. 【深度学习】百度:YOLOX和NanoDet都没我优秀!轻量型实时目标检测模型PP-PicoDet开源...
  5. mysql如何防止插入重复数据_如何防止MySQL重复插入数据,这篇文章会告诉你
  6. 酷炫星空公告维护页源码
  7. docker用gpu的参数_初探Docker调用GPU
  8. Lua 正确的尾调用(proper tail call)
  9. (C# TCP异步)客户端异常断开,服务器利用KeepAlive监测
  10. 《TCP/IP详解卷1》学习小结(一)------链接层
  11. 文华赢顺显示连接服务器失败,文华财经赢顺云本机已被系统限制登陆修复工具...
  12. python泰坦尼克号数据分析_利用python分析泰坦尼克号数据集
  13. 配置PotPlayer和Dolby Access启用耳机杜比全景声
  14. 一键获取网盘提取码的神器“云盘万能钥匙”
  15. 云计算机每秒网速,云游戏网速要求是多少_云游戏网速不够怎么办
  16. JSON校验和JSON在线编辑器
  17. mysql里面的时间函数
  18. gel和react哪个厉害_都是顶级缓震,亚瑟士GEL和耐克React谁更胜一筹?__拼团团购网...
  19. 博图V14安装(无图流程)S7-1200
  20. UNIX网络编程unp.h配置

热门文章

  1. HH SaaS电商系统的商品系统设计
  2. 在java中3.14156d表示_2006年9月计算机等级考试二级Java笔试真题
  3. sql 计算两个小数乘积_数学篇|学会这些数学计算技巧,想不满分都难!
  4. zynq+linux固化程序,如何在 Zynq UltraScale+ MPSoC 上实现 Linux UIO 设计
  5. keepalived安装与配置_Nginx_Keepalived高可用配置
  6. nginx哪个版本性能好_nginx性能为什么好
  7. oracle 48小时内_缺血性脑梗死后48小时内使用阿替普酶能够降低脑损伤程度
  8. python利用什么模块_什么是Python的heapq模块?
  9. 嵌入式C语言的7个硬核知识
  10. C/C++打造Windows岁月留声机