简体   繁体   中英

How to enable OWIN logging on Windows 7

I am trying to implement logging into my OWIN self hosting solution.

My MiddleWare class is as follows:

public class HostingMiddleware : OwinMiddleware
{
    private readonly ILogger _logger;

    public HostingMiddleware(OwinMiddleware next, IAppBuilder builder)
        : base(next)
    {
        _logger = builder.CreateLogger<HostingMiddleware>();
    }

    public override Task Invoke(IOwinContext context)
    {
        _logger.WriteVerbose(string.Format("{0} {1}: {2}"));
        context.Response.Headers.Add("Content-Type", new[] 
                                                     { 
                                                        "text/plain"
                                                     });
        return Invoke(context);
    }
}

I then use this in my Startup class.

public class Startup
{
   public void Configuration(IAppBuilder builder)
   {
      // Initialize the configuration for Web API self-host.
      HttpConfiguration config = new HttpConfiguration();

      // Map the default Web API HTTP Route
      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

      // Use Web API
      builder.UseWebApi(config);

      builder.Use<HostingMiddleware>(builder);
   }
}

When I remove builder.Use<HostingMiddleware>(builder); I can make a HTTP request and get a response, however when I implement my Middleware class I get:

No WebSocket support detected, Windows 8 or later is required.

From there forward, the logging doesn't work as it should do. Surely there is a way to get logging to work on a Windows 7 environment otherwise the functionality is rendered pretty useless? Any ideas on what I need to do?

Thanks in advance.

You need to use Next.Invoke(context) and not what you are doing.

Also you had a "bug" in the logging - I don't know what did you want to log so - I add my own.

Good luck with oWin - I love it.


public class HostingMiddleware : OwinMiddleware
{
    private readonly ILogger _logger;

    public HostingMiddleware(OwinMiddleware next, IAppBuilder builder)
        : base(next)
    {
        _logger = builder.CreateLogger();
    }

    public async override Task Invoke(IOwinContext context)
    {
        _logger.WriteVerbose(string.Format("{0} --- {1}", context.Request.Uri.AbsolutePath, context.Request.QueryString);
        await Next.Invoke(context);
    }
}

you must read this: Understanding OWIN, Katana, and the Middleware Pipeline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM