And*_*rii 4 .net logging enterprise-library
我需要执行日志记录到控制台(或调试/跟踪).目前我正在使用Ent Lib 4.1 Logging Application Block.要登录到控制台,我使用SystemDiagnosticsTraceListenerData和System.Diagnostics.ConsoleTraceListener.
它可以很好地执行日志记录到控制台,但是我无法为此lister类型使用格式化程序,因此无法将日志条目格式化为所需的格式.我需要的只是日志消息,没有默认提供的所有附加信息(这使得日志对我的情况不太可读).
我是否缺少任何配置选项来启用SystemDiagnosticsTraceListener的格式化?
实现此目的的一种方法是创建自定义TraceListener.然后在app.config中将listnerDataType设置为CustomTraceListenerData.这允许ConsoleTraceListener具有格式化输出.输出格式符合预期.如果没有格式化程序,则返回所有值.自定义TraceListener和app.config都已附加.
代码使用5.0.505.0而不是原始问题提出的4.1.
此代码取自此站点:java2s firstbricks»FirstBricks»EntLib»Logging»Extensions»ConsoleTraceListener.cs(cache).删除了注释以使代码更容易在StackOverflow上读取,并且默认颜色从白色更改为灰色.
namespace Eab.Logging
{
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class ConsoleTraceListener : CustomTraceListener
{
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if (data is LogEntry && Formatter != null) {
LogEntry le = (LogEntry)data;
WriteLine(Formatter.Format(le), le.Severity);
} else {
WriteLine(data.ToString());
}
}
public override void Write(string message)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(message);
}
public override void WriteLine(string message)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(message);
}
public void WriteLine(string message, TraceEventType severity)
{
ConsoleColor color;
switch (severity) {
case TraceEventType.Critical:
case TraceEventType.Error:
color = ConsoleColor.Red;
break;
case TraceEventType.Warning:
color = ConsoleColor.Yellow;
break;
case TraceEventType.Information:
color = ConsoleColor.Cyan;
break;
case TraceEventType.Verbose:
default:
color = ConsoleColor.Gray;
break;
}
Console.ForegroundColor = color;
Console.WriteLine(message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<!-- Namespace+class, applicationName -->
<add
type="Eab.Logging.ConsoleTraceListener, Eab.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
formatter="Simple Formatter"
name="Console Listener" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="{timestamp(local:[MM/dd/yyyy HH:mm:ss.fff])} : ({title}) {message}"
name="Simple Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Console Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="Console Listener" />
</listeners>
</notProcessed>
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Console Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3510 次 |
| 最近记录: |