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.

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
  • 1
    I'm unable to reproduce this issue when I try it with a simple example without using WebAPI. Are you sure your request is not handled by WebApi and it reaches until the HostingMiddleware? Or is your intent to have this HostingMiddleware actually log every request to this WebAPI and add a response header - If so try registering the middleware before the UseWebApi(config). – Praburaj Dec 06 '13 at 19:17
  • one thing that is just wrong is "return Invoke(context);" that will just cause a giant loop of the same call over and over. – awright18 Mar 13 '15 at 17:11

1 Answers1

5

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

Einav Hacohen
  • 787
  • 5
  • 14