在TraceListener的自我OWIN托管 [英] TraceListener in OWIN Self Hosting

查看:120
本文介绍了在TraceListener的自我OWIN托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Microsoft.Owin.Hosting 来承载以下,非常简单的Web应用程序。



下面是启动它的呼叫:



<预类=郎-CS prettyprint-覆盖> WebApp.Start< PushServerStartup>(HTTP://本地主机:8080 /事件);

下面是我使用启动类:



<预类=郎-CS prettyprint-覆盖> 公共类PushServerStartup
{
公共无效配置(IAppBuilder应用程序)
{
app.MapHubs ();
}
}



我运行这个控制台应用程序,做了内很多其他的事情,包括路由跟踪写入某些文件等等。但是突然间(激活OWIN主机时),我看到写的是正常路由别的地方控制台跟踪消息。



显然,有活跃在OWIN托管框架一些跟踪侦听器。我怎样才能将其关闭?


解决方案

我找到了自己的解决方案。学习武士刀的源代码之后好像你需要注册自己的 ITraceOutputFactory 实例否决默认跟踪监听器(这是写入控制台)。



下面是新的开始呼叫:



<预类=郎-CS prettyprint-覆盖> VAR dummyFactory =新DummyFactory();
VAR提供商= ServicesFactory.Create(
defaultServiceProvider => defaultServiceProvider.AddInstance< ITraceOutputFactory>(dummyFactory));

使用(WebApp.Start<&启动GT;(供应商,新StartOptions(HTTP://本地主机:8090)))
{
到Console.ReadLine();
}

这是一个虚拟的跟踪厂(也许不是最好的解决办法,但你可以



<预类=郎-CS prettyprint-覆盖> 公共类DummyFactory:ITraceOutputFactory $ b的东西服务的宗旨更好一点)替换它$ b {
公众的TextWriter创建(字符串OUTPUTFILE)
{
返回新的StringWriter();
}
}


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

Here is the call to start it:

WebApp.Start<PushServerStartup>("http://localhost:8080/events");

Here is the startup class I am using:

public class PushServerStartup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapHubs();
    }
}

I am running this inside a console application that does a lot of other things including routing trace writing to certain files etc. But all of a sudden (when activating the OWIN hosting) I am seeing trace messages written to the console that are normally routed somewhere else.

Obviously there are some trace listeners active in the OWIN hosting framework. How can I switch them off?

解决方案

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 new StringWriter();
    }
}

这篇关于在TraceListener的自我OWIN托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆