asp.net-web-api – 随着SerilogWeb.Owin停产,是否有“官方”集
发布时间:2020-12-16 04:28:23 所属栏目:asp.Net 来源:网络整理
导读:我遇到了 the SerilogWeb.Owin package的 the discontinuation notice,并且在阅读GitHub问题时,考虑到包的下载量约为5K,有人讨论了“将人们重定向到某个地方”. 但我无法弄清楚我被重定向到哪里! 那么我应该在哪里寻找使用Serilog和(OWIN)自托管Web API的“
我遇到了
the SerilogWeb.Owin package的
the discontinuation notice,并且在阅读GitHub问题时,考虑到包的下载量约为5K,有人讨论了“将人们重定向到某个地方”.
但我无法弄清楚我被重定向到哪里! 那么我应该在哪里寻找使用Serilog和(OWIN)自托管Web API的“Serilog-blessed”集成? 解决方法
该软件包归结为以下内容,我从[repo](
https://github.com/serilog-web/owin/blob/master/src/SerilogWeb.Owin/Owin/LoggerFactory.cs
): >添加RequestId以允许跟踪相关 using (Serilog.Context.LogContext.PushProperty("RequestId",Guid.NewGuid().ToString("N")) >插入记录器重定向器: app.SetLoggerFactory(new SerilogOwinFactory()); >复制in impl(这适用于WebApi 5.2.4,Katana 4.0,Serilog 2.6)和incorporates learnings from another question关于有效转发事件而不将写入视为模板,并确保将Exception包含在消息的第一类元素中命令它可以格式化和/或demystyfied正确: 好消息是,当你到达ASP.NET Core时,会有一个完全维护的包,有一个更深层次的集成等着你,一个单行的钩子;) // From https://github.com/serilog-web/owin/blob/master/src/SerilogWeb.Owin/Owin/LoggerFactory.cs // Copyright 2015 SerilogWeb,Serilog Contributors // // Licensed under the Apache License,Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing,software // distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using System; using System.Diagnostics; using System.Linq; using Microsoft.Owin.Logging; using Serilog; using Serilog.Core; using Serilog.Events; using ILogger = Serilog.ILogger; namespace SerilogWeb.Owin { /// <summary> /// Implementation of Microsoft.Owin.Logger.ILoggerFactory. /// </summary> public class SerilogOwinFactory : ILoggerFactory { readonly Func<ILogger> _getLogger; readonly Func<TraceEventType,LogEventLevel> _getLogEventLevel; /// <summary> /// Create a logger factory. /// </summary> /// <param name="logger">The logger; if not provided the global <see cref="Serilog.Log.Logger"/> will be used.</param> /// <param name="getLogEventLevel"></param> public SerilogOwinFactory(ILogger logger = null,Func<TraceEventType,LogEventLevel> getLogEventLevel = null) { _getLogger = logger == null ? (Func<ILogger>)(() => Log.Logger) : (() => logger); _getLogEventLevel = getLogEventLevel ?? ToLogEventLevel; } /// <summary> /// Creates a new ILogger instance of the given name. /// </summary> /// <param name="name">The logger context name.</param> /// <returns>A logger instance.</returns> public Microsoft.Owin.Logging.ILogger Create(string name) { return new Logger(_getLogger().ForContext(Constants.SourceContextPropertyName,name),_getLogEventLevel); } static LogEventLevel ToLogEventLevel(TraceEventType traceEventType) { switch (traceEventType) { case TraceEventType.Critical: return LogEventLevel.Fatal; case TraceEventType.Error: return LogEventLevel.Error; case TraceEventType.Warning: return LogEventLevel.Warning; case TraceEventType.Information: return LogEventLevel.Information; case TraceEventType.Verbose: return LogEventLevel.Verbose; case TraceEventType.Start: return LogEventLevel.Debug; case TraceEventType.Stop: return LogEventLevel.Debug; case TraceEventType.Suspend: return LogEventLevel.Debug; case TraceEventType.Resume: return LogEventLevel.Debug; case TraceEventType.Transfer: return LogEventLevel.Debug; default: throw new ArgumentOutOfRangeException("traceEventType"); } } class Logger : Microsoft.Owin.Logging.ILogger { readonly ILogger _logger; readonly Func<TraceEventType,LogEventLevel> _getLogEventLevel; static readonly Exception _exceptionPlaceHolder = new Exception("(Exception enclosed)"); internal Logger(ILogger logger,LogEventLevel> getLogEventLevel) { _logger = logger; _getLogEventLevel = getLogEventLevel; } public bool WriteCore(TraceEventType eventType,int eventId,object state,Exception exception,Func<object,Exception,string> formatter) { var level = _getLogEventLevel(eventType); // According to docs http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin/Logging/ILogger.cs // "To check IsEnabled call WriteCore with only TraceEventType and check the return value,no event will be written." if (state == null) return _logger.IsEnabled(level); if (!_logger.IsEnabled(level)) return false; var formattedMessage = formatter(state,null); // Omit exception as we're including it in the LogEvent var template = new Serilog.Events.MessageTemplate(new[] { new Serilog.Parsing.TextToken(formattedMessage) }); var logEvent = new Serilog.Events.LogEvent(DateTimeOffset.Now,level,exception,template,Enumerable.Empty<Serilog.Events.LogEventProperty>()); _logger.ForContext("eventId",eventId).Write(logEvent); return true; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何从asp.net MVC 4应用程序中的FileResul
- ASP.NET MVC – 使用ViewData将Json String传递给View
- .net – ServiceHost和WebServiceHost有什么区别?
- asp.net – 引用和外部类使用问题
- 将生产服务器从ASP.NET 2升级到ASP.NET 4
- Task.Factory.StartNew在ASP.Net MVC中有帮助或有害吗?
- asp.net-mvc – 获取错误 – “@”字符后面的意外“if”关键
- asp.net-mvc – 强大类型的ASP.NET MVC与ADO.NET实体框架
- 你可以在ASP.NET MVC的页面上有两个表单吗?
- .net – FileLoadException:无法加载文件或程序集System.R
推荐文章
站长推荐
- 在asp.net后面的代码中创建javaScript变量
- 使用ASP.NET窗体身份验证的WCF服务
- asp.net-mvc – ConfigurationManager读取错误的
- asp.net-mvc – ASP.NET MVC:如何处理View中的N
- asp.net-mvc – System.Web.Optimization使函数参
- asp.net-mvc – 什么是实例化和配置DbContext在M
- asp.net-mvc – 限制每个动作的HTTP动词
- asp.net-mvc – 在ASP.NET MVC中处理控件显示逻辑
- asp.net+ajaxfileupload.js 实现文件异步上传代码
- ASP.NET 5中的全局异常处理
热点阅读