4

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.

4

1 回答 1

5

你需要使用Next.Invoke(context)而不是你正在做什么。

此外,您在日志记录中有一个“错误” - 我不知道您想要记录什么 - 我添加了我自己的。

祝 oWin 好运——我喜欢它。


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);
    }
}

你必须阅读这篇文章:了解 OWIN、Katana 和中间件管道

于 2015-06-28T11:33:35.507 回答