csharp 将LibLog与Microsoft.Owin.Logging一起使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 将LibLog与Microsoft.Owin.Logging一起使用相关的知识,希望对你有一定的参考价值。

namespace LigLogOwin
{
    using System;
    using System.Diagnostics;
    using LibLogOwin.Logging;
    using Microsoft.Owin.Logging;
    using Owin;

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.SetLoggerFactory(new LibLogLoggerFactory());
        }

        private class LibLogLoggerFactory : ILoggerFactory
        {
            public ILogger Create(string name)
            {
                return new LibLogLogger(LogProvider.GetLogger(name));
            }

            private class LibLogLogger : ILogger
            {
                private readonly ILog _logger;

                public LibLogLogger(ILog logger)
                {
                    _logger = logger;
                }

                public bool WriteCore(
                    TraceEventType eventType,
                    int eventId,
                    object state,
                    Exception exception,
                    Func<object, Exception, string> formatter)
                {
                    return state == null
                        ? _logger.Log(Map(eventType), null) // Equivalent to IsLogLevelXEnabled 
                        //TODO What to do with eventId?
                        : _logger.Log(Map(eventType), () => formatter(state, exception), exception); 
                }

                private LogLevel Map(TraceEventType eventType)
                {
                    switch(eventType)
                    {
                        case TraceEventType.Critical:
                            return LogLevel.Fatal;
                        case TraceEventType.Error:
                            return System.LogLevel.Error;
                        case TraceEventType.Warning:
                            return LogLevel.Warn;
                        case TraceEventType.Information:
                            return LogLevel.Info;
                        case TraceEventType.Verbose:
                            return System.LogLevel.Trace;
                        case TraceEventType.Start:
                            return LogLevel.Info;
                        case TraceEventType.Stop:
                            return LogLevel.Info;
                        case TraceEventType.Suspend:
                            return LogLevel.Info;
                        case TraceEventType.Resume:
                            return LogLevel.Info;
                        case TraceEventType.Transfer:
                            return LogLevel.Info;
                        default:
                            throw new ArgumentOutOfRangeException("eventType");
                    }
                }
            }
        }
    }
}

以上是关于csharp 将LibLog与Microsoft.Owin.Logging一起使用的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 中常用的 so 动态库 ( libm.so 数学函数动态库 | liblog.so 日志模块动态库 | libselinux.so 安全模块动态库 )(

关于异常Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

错误:*.csproj 无法在 MonoDevelop 上导入“$(MSBuildBinPath)\Microsoft.CSharp.targets”

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

csharp 如何使用continue运算符的示例。来自Microsoft C#Reference。 https://docs.microsoft.com/en-us/dotnet/csharp/la