随着SerilogWeb.Owin的停产,是否有一个“官方"网站?一体化? [英] With SerilogWeb.Owin discontinued, is there an "official" integration?

查看:57
本文介绍了随着SerilogWeb.Owin的停产,是否有一个“官方"网站?一体化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了停产通知 .nu​​get.org/packages/serilogweb.owin"rel =" nofollow noreferrer> SerilogWeb.Owin程序包,在阅读GitHub问题时,讨论了将人重定向到某处"的问题,因为下载了〜5K +包.

I came across the discontinuation notice of the SerilogWeb.Owin package, and in reading the GitHub issue there was discussion about "redirecting folks somewhere" given the ~5K+ downloads of the package.

但是我无法弄清楚我被重定向到的位置!

But I haven't been able to figure out where I'm being redirected to!

那么我应该在哪里寻找将Serilog与(OWIN)自托管Web API结合使用的受Serilog祝福"的集成?

So where should I be looking for a "Serilog-blessed" integration for using Serilog with an (OWIN) self-hosted Web API?

推荐答案

该软件包归结为以下内容,我从[repo](

The package boils down to the following, which I derived from the issues in [the repo](https://github.com/serilog-web/owin/blob/master/src/SerilogWeb.Owin/Owin/LoggerFactory.cs ):

  1. 添加一个RequestId以允许跟踪关联

  1. Adding a RequestId to allow traces to be correlated

using (Serilog.Context.LogContext.PushProperty("RequestId", Guid.NewGuid().ToString("N"))

  • 插入记录器重定向器:

  • Plug in a logger redirector:

    app.SetLoggerFactory(new SerilogOwinFactory());
    

  • 在impl中复制(这适用于WebApi 5.2.4,Katana 4.0,Serilog 2.6)和

  • Copy in the impl (this works with WebApi 5.2.4, Katana 4.0, Serilog 2.6) and incorporates learnings from another question about efficiently forwarding events without the writing treating the message as a template, and ensuring the Exception is included a first class element of the message in order that it can be formatted and/or demystyfied corrrectly:

    好消息是,当您到达ASP.NET Core时,将有一个完全维护的程序包,它具有更深层次的集成,等待您的到来,并且具有单线挂钩;)

    And the good news is that, when you reach ASP.NET Core, there will be a fully maintained package with a deeper integration waiting for you, with a one-liner hookin ;)

    // 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, Func<TraceEventType, 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;
                }
            }
        }
    }
    

    这篇关于随着SerilogWeb.Owin的停产,是否有一个“官方"网站?一体化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆