TraceListener in OWIN Self Hosting

前端 未结 3 788
谎友^
谎友^ 2020-12-05 23:51

I am using Microsoft.Owin.Hosting to host the following, very simple web app.

Here is the call to start it:



        
相关标签:
3条回答
  • 2020-12-06 00:35

    I found a solution myself. After studying the Katana source code it seems like you need to register your own ITraceOutputFactory instance to overrule the default trace listener (which is writing to the console).

    Here is the new start call:

    var dummyFactory = new DummyFactory();
    var provider = ServicesFactory.Create(
        defaultServiceProvider => defaultServiceProvider.AddInstance<ITraceOutputFactory>(dummyFactory));
    
    using (WebApp.Start<Startup>(provider, new StartOptions("http://localhost:8090")))
    {
        Console.ReadLine();
    }
    

    And here is a dummy trace factory (maybe not the best solution but you can replace it with something serving your purpose a little better):

    public class DummyFactory : ITraceOutputFactory
    {
        public TextWriter Create(string outputFile)
        {
            return TextWriter.Null;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 00:36

    I had the same issue, I was self hosting 4 instances in one process and for each request was getting 4 lots of messages traced to console.

    I simply removed the TraceListener instance

    Trace.Listeners.Remove("HostingTraceListener")
    

    "HostingTraceListener" is defined in the owin source code so I guess could change - http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Hosting/Engine/HostingEngine.cs

    I did this after

    WebApp.Start(...
    
    0 讨论(0)
  • 2020-12-06 00:36

    An alternative to the answer from meilke that works with latest Katana self-host (2.1.0):

    StartOptions options = new StartOptions("http://localhost:8080/events");
    
    // disable built-in owin tracing by using a null traceoutput
    options.Settings.Add(
        typeof(Microsoft.Owin.Hosting.Tracing.ITraceOutputFactory).FullName,
        typeof(NullTraceOutputFactory).AssemblyQualifiedName);
    
    using (WebApp.Start<PushServerStartup>(options))
    

    NullTraceOutputFactory is similar to DummyFactory but using StreamWriter.Null instead of StringWriter:

    public class NullTraceOutputFactory : ITraceOutputFactory
    {
        public TextWriter Create(string outputFile)
        {
            return StreamWriter.Null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题