ExceptionLogger
应用1:webconfig.cs中设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi" , routeTemplate: "api/{controller}/{id}" , defaults: new { id = RouteParameter.Optional } ); //error log GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; config.Services.Add( typeof (IExceptionLogger), new ApiExceptionLogger()); } } |
应用二:Global.asax.cs
1 2 3 4 5 6 7 8 9 | protected void Application_Error( object sender, EventArgs e) { Response.Filter = null ; var lastError = Server.GetLastError(); if (lastError != null ) { _logger.Error(lastError, "CRM site exception" ); } } |
ApiExceptionLogger.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class ApiExceptionLogger: ExceptionLogger { public ApiExceptionLogger(){} public override void Log(ExceptionLoggerContext context) { StringBuilder sb = new StringBuilder(); var r = context.Request; if (r != null ) { sb.AppendLine($ "Unhandled exception processing {r.Method} for {r.RequestUri}" ); if (r.Headers.Count() > 0) { sb.AppendLine( "Headers Begin:" ); sb.AppendLine($ "{r.Headers.ToString()}" ); sb.AppendLine( "Headers End." ); } if (r.Content!= null ) { var content = r.Content.ReadAsStringAsync().Result; sb.AppendLine( "Contents Begin:" ); sb.AppendLine(content); sb.AppendLine( "Contents End." ); } } IIocHelper ioc = IocFactory.Instance; var logger = ioc.GetInstance<ILogBase>(); logger.Error(sb.ToString(), context.Exception); } } |
logger:可以使用NLog实现
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?