Microsoft Owin日志记录-Web Api 2-如何创建记录器? [英] Microsoft Owin Logging - Web Api 2 - How do I create the logger?

查看:221
本文介绍了Microsoft Owin日志记录-Web Api 2-如何创建记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web Api 2和Owin将日志记录添加到我的应用程序中,因此我开始使用Microsoft Owin Logging,它需要已实现的ILoggerILoggerFactory,并且在需要时可以很好地工作记录STARTUP方法中的任何内容或任何Owin中间件组件.

I am trying to add logging to my app using Web Api 2 and Owin, so I started using Microsoft Owin Logging, which requires an ILogger and ILoggerFactory, that has been implemented and it works great when I need to log anything inside the STARTUP method or any of the Owin Middleware components.

例如,当我使用Startup方法时,可以使用以下命令创建记录器:

For example, when I am in the Startup method I can create the logger using:

    public void Configuration(IAppBuilder app)
    {
        // Creates configuration
        var configuration = new HttpConfiguration();

        // Configure WebApi Settings
        WebApiConfig.Register(configuration);

        app.SetLoggerFactory(new OwinLog4NetLoggerFactory("Default"));

        var logger = app.CreateLogger<Startup>();
        logger.WriteInformation("test log");

        // Enabled WebApi in OWIN
        app.UseWebApi(configuration);
    }

其中"OwinLog4NetLoggerFactory"是我的自定义ILoggerFactory实现.

Where "OwinLog4NetLoggerFactory" is my custom ILoggerFactory implementation.

到目前为止,很好...但是...当我使用实际的Web api操作方法时如何创建记录器?...我尝试访问Request.GetOwinEnvironment()并且记录器工厂不在字典.

So far, so good... but... How can I create the logger when I am in the actual web api action method?... I tried accessing the Request.GetOwinEnvironment() and the logger factory is not in the dictionary.

例如:

public class AccountController : ApiController
{
    public int Get(int id)
    {
        // Create logger here

        return id + 1;
    }
}

我知道我可以使用对Logger Factory甚至是Injection的引用创建一个静态类,以将logger添加到api控制器,但这对于应该已经存在的东西来说似乎太复杂了.

I know I can create a static class with a reference to the Logger Factory or even Injection to add the logger to the api controller, but that seems too complicated for something that should be already there.

任何想法都会受到赞赏.

Any ideas would be appreciated.

推荐答案

我建议编写中间件,以便您可以在控制器外部处理日志记录:

I'd recommend writing your middleware so that you can handle the logging outside of the controller:

public class LoggingMiddleware : OwinMiddleware
{
    public LoggingMiddleware(OwinMiddleware next)
        : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
            //handle request logging

            await Next.Invoke(context); 

            //handle response logging
    }

}

然后在Startup类中:

Then in Startup class:

public class Startup
{
    // ReSharper disable once UnusedMember.Global
    public void Configuration(IAppBuilder appBuilder)
    {
        HttpConfiguration config = new HttpConfiguration();

        config.MapHttpAttributeRoutes();
        appBuilder.Use<LoggingMiddleware>();
        appBuilder.UseWebApi(config);
    }
}

然后,请求将进入,在LoggingMiddleware中命中请求日志代码,在控制器代码中命中,然后响应将在返回的途中记录在LoggingMiddleware中.

The request would then come in, hit the request logging code in the LoggingMiddleware, hit the controller code and then response would be logged on the LoggingMiddleware on the way back.

但是,如果您要做的只是将对象从中间件发送到控制器,则可以在中间件中使用context.Set("loggingObject", loggingObject);,然后 var loggingObject = Request.GetOwinContext().Get<LoggerClass>("loggingObject");在控制器中.

However, if all you are wanting to do is send an object through from middleware to the controller you can use context.Set("loggingObject", loggingObject); in the middleware and then var loggingObject = Request.GetOwinContext().Get<LoggerClass>("loggingObject"); in the controller.

这篇关于Microsoft Owin日志记录-Web Api 2-如何创建记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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