我有2个WCF服务,我从一个Windows主机托管。我使用跟踪侦听器将数据记录到应用程序日志中。我已将以下代码添加到配置文件中。
<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名称下。我想要做的是,来自每个服务的日志应该出现在不同的源名称下。
例如,来自ReaderService的日志应出现在名称ReaderServiceLog下,InfoService中的日志应出现在InfoServiceLog下。我修改了我的配置,如下所示。
<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>
并使用此代码:
private TraceSource ts = new TraceSource("InfoService");
ts.TraceInformation(outputMessage, aslErrorText);
ts.Flush();
但它不起作用。它根本不记录任何内容。
我也试过this。但它不起作用。
<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#代码。此代码正确执行日志记录,但同样,这两个服务的名称相同。即ServiceLog。
我在这里遗漏了什么吗?或者还有其他办法吗? 请帮忙
答案 0 :(得分:2)
此配置添加了2个不同的跟踪源(使用文件侦听器;如果您愿意,可能需要更改侦听器和目录路径):
<?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:
static TraceSource dataSource = new TraceSource("DataSource");
static TraceSource errorSource = new TraceSource("ErrorSource");
为了使用TraceSource更轻松地(对于某些场景),我编写了一个扩展方法:
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);
}
这对我有用。
但是,您无法根据调用它的代码块在一个应用程序域中对TraceSource进行分类。