Using the host-provided "host.TraceOutput" writer on OWIN hosts ? #2260

Closed
KrzysFR opened this Issue Jul 5, 2013 · 6 comments

Projects

None yet

3 participants

@KrzysFR
Contributor
KrzysFR commented Jul 5, 2013

Is it possible to have SignalR make use of the "host.TraceOutput" TextWriter provided by the OWIN host ? (like for example, a simple command line OWIN host that would log all traces to Console.Out)

Using the latest 2.0.0-beta2 version from Nuget, and when hosted on a custom OWIN host with a breakpoint set in the "IDictionary<string, object>TryGetValue(...)" implementation of the OWIN environment passed to the SignalRMiddleware.Invoke(..) method, the constant "host.TraceOutput" is never used.

Also, the only instance of "TraceOutput" I can find in SignalR "core" the code seems to be in OwinConstant.cs but it is never referenced by anyone. Other instances seems to be related to test code or in the Client.

Looking at the dependency resolver, it would seem like one would need to provide a custom ITraceManager to handle logging, but I'm not sure how you can create a custom "OwinAwareTraceManager" that would be able to get a hold of the per-request OWIN environment dictionary to get the correct TextWriter ?

Or am I missing something ?

Edit: just to be sure, I'm talking about the "host.TraceOutput" from there http://owin.org/spec/CommonKeys.html#_6._Common_keys

@davidfowl
Member

You can do this for now as a workaround.

public class OwinTraceManager : ITraceManager
{
    private readonly ConcurrentDictionary<string, TraceSource> _sources = new ConcurrentDictionary<string, TraceSource>(StringComparer.OrdinalIgnoreCase);

    private readonly TextWriter _hostWriter;

    public OwinTraceManager(TextWriter hostWriter)
    {
        Switch = new SourceSwitch("SignalRSwitch");
        _hostWriter = hostWriter;
    }

    public SourceSwitch Switch { get; private set; }

    public TraceSource this[string name]
    {
        get
        {
            return _sources.GetOrAdd(name, key => CreateTraceSource(key));
        }
    }

    private TraceSource CreateTraceSource(string name)
    {
        var traceSource = new TraceSource(name, SourceLevels.Off)
        {
            Switch = Switch
        };

        traceSource.Listeners.Add(new TextWriterTraceListener(_hostWriter));

        return traceSource;
    }
}
public void Configuration(IAppBuilder app)
{
    var writer = (TextWriter)app.Properties["host.TraceOutput"];

    var traceManager = new OwinTraceManager(writer);
    GlobalHost.DependencyResolver.Register(typeof(ITraceManager), () => traceManager);

    traceManager.Switch.Level = SourceLevels.All;

    app.MapConnection<RawConnection>("/raw-connection", new ConnectionConfiguration { EnableCrossDomain = true });
}

I'll put this in 2.0 so that we automatically add a trace listener for the host.TraceOutput if available.

@davidfowl davidfowl was assigned Jul 5, 2013
@KrzysFR
Contributor
KrzysFR commented Jul 5, 2013

Thanks, looks like it should work. I'll give it a try.

@KrzysFR
Contributor
KrzysFR commented Jul 5, 2013

Yep, it's working fine. I adapted the code because in my specific case I'm getting the TextWriter some other way.

Anyway, I'm seeing the SignalR traces going out on the Console, and I can control them by setting the SourceSwitch level.

Only drawback I can see, is that if the host.TraceOutput changes at sometimes during the lifetime of the server (settings being changed in realtime without host reboot?) then the OwinTraceManager will keep using the previous one.

Not sure if this is a big deal or not.

@davidfowl
Member

Things that exist in the appBuilder.Properties have a per app domain lifetime not a per request lifetime. We would never support changing it per the trace or any other of the host specific keys per request.

@davidfowl davidfowl added a commit that referenced this issue Jul 6, 2013
@davidfowl davidfowl Setup a default trace listener if the host provides a "host.TraceOutput"
- Turned tracing on in self host sample.
#2260
2b854a4
@davidfowl davidfowl added a commit that referenced this issue Jul 6, 2013
@davidfowl davidfowl Setup a default trace listener if the host provides a "host.TraceOutput"
- Turned tracing on in self host sample.
#2260
1585ca6
@davidfowl davidfowl added a commit that referenced this issue Jul 9, 2013
@davidfowl davidfowl Setup a default trace listener if the host provides a "host.TraceOutput"
- Turned tracing on in self host sample.
#2260
5d66ee5
@Xiaohongt Xiaohongt was assigned Jul 9, 2013
@Xiaohongt
Contributor

In web app and SignalR trace sources are added in web.config, now when debug the web app, in VS output it always have duplicate SignalR traces.

@davidfowl davidfowl was assigned Jul 19, 2013
@Xiaohongt Xiaohongt was assigned Jul 20, 2013
@Xiaohongt
Contributor

verified trace in both Asp.net host and self host and output for trace when debug.

@Xiaohongt Xiaohongt closed this Jul 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment