使用一个以上的跟踪侦听器 [英] Using more than one trace listeners
问题描述
我有我从一个单一的windows主机托管WCF 2服务。我使用跟踪监听器将数据记录到应用程序日志。我加入了以下code到配置文件。
I have 2 WCF services which I am hosting from a single windows host. I use trace listener which logs the data into application logs. I have added following code into the config file.
<system.diagnostics>
<switches>
<add name="ReaderService.Switch" value="4"/>
<add name="InfoService.Switch" value="4"/>
</switches>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="EventLogTraceListener"
type="System.Diagnostics.EventLogTraceListener"
initializeData="ReaderServiceLog" />
</listeners>
</trace>
</system.diagnostics>
所有无论从服务日志出现在源ReaderServiceLog名。我想要做的是,从每个服务日志中应该会出现在不同的源名称。
All the logs from both the services appear under source ReaderServiceLog name. What I want to do is, logs from each service should appear under different source name.
例如,从ReaderService日志应该出现在名称ReaderServiceLog从InfoService中日志应InfoServiceLog下。我修改我的配置如下所示。
For example, Logs from ReaderService should appear under name ReaderServiceLog and logs from InfoService should appear under InfoServiceLog. I modified my config like the following.
<system.diagnostics>
<switches>
<add name="ReaderService.Switch" value="4"/>
<add name="InfoService.Switch" value="4"/>
</switches>
<sources>
<source name="EventLogTraceListener">
<listeners>
<add name="EventLogTraceListener"
type="System.Diagnostics.EventLogTraceListener"
initializeData="ReaderServiceLog" />
</listeners>
</source>
<source name="InfoService">
<listeners>
<add name="EventLogTraceListener"
type="System.Diagnostics.EventLogTraceListener"
initializeData="InfoServiceLog" />
</listeners>
</source>
</sources>
</system.diagnostics>
和使用该code:
private TraceSource ts = new TraceSource("InfoService");
ts.TraceInformation(outputMessage, aslErrorText);
ts.Flush();
但它不工作。它根本不记录任何。
But it does not work. It does not log anything at all.
我也试过<一个href=\"http://stackoverflow.com/questions/805154/defining-multiple-tracesources-not-running?lq=1\">this.但是,这是行不通的。
I also tried this. But it does not work.
<system.diagnostics>
<switches>
<add name="ReaderService.Switch" value="4"/>
<add name="InfoService.Switch" value="4"/>
</switches>
<sources>
<source name="ReaderService"
switchValue="Information, ActivityTracing">
<listeners>
<add name="EventLogTraceListener"/>
</listeners>
</source>
<source name="InfoService"
switchValue="Information, ActivityTracing">
<listeners>
<add name="EventLogTraceListener"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="EventLogTraceListener"
type="System.Diagnostics.EventLogTraceListener"
initializeData="ServiceLog" />
</sharedListeners>
我用同样的C#code同上。这code确实记录正确的,但同样,这是根据同名两种服务。即ServiceLog。
I used the same c# code as above. This code does the logging properly, but again, It's under the same name for both the services. i.e. ServiceLog.
我失去了一些东西在这里?或者是有各地的任何其他方式。
请帮助
Am I missing something here? OR Is there any other way around. Please help
推荐答案
此配置增加了2个不同的跟踪源(随着文件的听众,你可能要更改监听器和目录路径,如果你喜欢):
This config adds 2 different trace sources (With file listeners; You may want to change listeners and directory path if you like):
<?xml version="1.0"?>
<configuration>
...
<system.diagnostics>
<switches>
<add name="data" value="All" />
<add name="error" value="All" />
</switches>
<sources>
<source name="DataSource" switchName="data" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<clear />
<add name="dataListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"
Append="true"
AutoFlush="true"
BaseFileName="data"
CustomLocation="D:\Log\App\Data"
DiskSpaceExhaustedBehavior="DiscardMessages"
Encoding="Unicode"
IncludeHostName="false"
LogFileCreationSchedule="Daily"
location="Custom"
MaxFileSize="900000000000" />
</listeners>
</source>
<source name="ErrorSource" switchName="error" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<clear />
<add name="errorListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"
Append="true"
AutoFlush="true"
BaseFileName="error"
CustomLocation="D:\Log\App\Error"
DiskSpaceExhaustedBehavior="DiscardMessages"
Encoding="Unicode"
IncludeHostName="false"
LogFileCreationSchedule="Daily"
location="Custom"
MaxFileSize="900000000000" />
</listeners>
</source>
</sources>
<trace autoflush="true">
<listeners>
<clear />
<add name="defaultListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"
Append="true"
AutoFlush="true"
BaseFileName="program"
CustomLocation="D:\Log\App\Program"
DiskSpaceExhaustedBehavior="DiscardMessages"
Encoding="Unicode"
IncludeHostName="false"
LogFileCreationSchedule="Daily"
location="Custom"
MaxFileSize="900000000000" />
<add name="programConsoleListener" type="System.Diagnostics.ConsoleTraceListener" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
和使用它定义TraceSource:
And to use it define your TraceSource:
static TraceSource dataSource = new TraceSource("DataSource");
static TraceSource errorSource = new TraceSource("ErrorSource");
要工作更容易(某些情况下)与TraceSource我已经写了一个扩展方法:
To work more easily (for some scenarios) with a TraceSource I've written an extension method:
public static void WriteLine(this TraceSource source, object o)
{
var str = (o ?? string.Empty).ToString();
if (source.Listeners == null || source.Listeners.Count == 0) throw new InvalidOperationException(string.Format("TraceSource named {0} has no listeners", source.Name));
foreach (TraceListener listener in source.Listeners)
listener.WriteLine(str);
}
这是为我工作。
不过,根据正在调用它code块在一个应用程序域不能归类TraceSource。
But you can not categorize TraceSource in one app domain based on the code block that is calling it.
这篇关于使用一个以上的跟踪侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!