问题描述
我试图用System.Diagnostics程序做了一些非常基本的日志记录。我想我会用什么在框中而不是采取一个额外的依赖像log4net的或EntLib。
I'm trying to use System.Diagnostics to do some very basic logging. I figure I'd use what's in the box rather than taking on an extra dependency like Log4Net or EntLib.
我都准备好了,跟踪工作出色。 code片断:
I'm all set up, tracing is working wonderfully. Code snippet:
Trace.TraceInformation("Hello World")
App.config中:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="TraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" traceOutputOptions="DateTime" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
和我的小的Hello World很好地出现在我的trace.log文件。但现在,我想开关关闭追查,让我钻进去MSDN和发现的。我添加了&LT;开关&GT;
元素,现在我的app.config看起来是这样的:
and my little "Hello World" shows nicely up in my Trace.log file. But now I'd like to switch OFF tracing, so I dig into MSDN and find How to: Configure Trace Switches. I add the <switches>
element, and now my app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="TraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" traceOutputOptions="DateTime" />
<remove name="Default" />
</listeners>
</trace>
<switches>
<add name="Data" value="0" />
</switches>
</system.diagnostics>
</configuration>
在值=0
应关闭跟踪 - 至少,如果你然后按照的,它告诉你要加入这一行的code:
The value="0"
should turn off tracing - at least if you then follow How to: Create and Initialize Trace Switches, which tells you to add this line of code:
Dim dataSwitch As New BooleanSwitch("Data", "DataAccess module")
这是没有意义的,我说:我只需要申报的一个实例 BooleanSwicth
来能够管理(禁用)通过config文件跟踪?我想... 使用的...对象的地方?
That doesn't make sense to me: I just have to declare an instance of the BooleanSwicth
to be able to manage (disable) tracing via the .config file? Should I like ... use ... the object somewhere?
不管怎么说,我敢肯定,我错过了一些东西真的很明显的地方。请大家帮帮忙。
Anyways, I'm sure I missed something really obvious somewhere. Please help.
如何开关转到OFF追查的app.config?
推荐答案
我同意@Alex汉弗莱的建议,尝试使用TraceSources。随着TraceSources你更好地控制你的日志/跟踪语句的执行方式。例如,你可以有code是这样的:
I agree with @Alex Humphrey's recommendation to try using TraceSources. With TraceSources you gain more control over how your logging/tracing statements execute. For example, you could have code like this:
public class MyClass1
{
private static readonly TraceSource ts = new TraceSource("MyClass1");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
public class MyClass2
{
private static readonly TraceSource ts = new TraceSource("MyClass2");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
在TraceSource.TraceEvent电话将自动检查邮件(TraceEventType.Information)的水平对相关交换机的配置水平,将决定是否该消息实际上应该写出来。
The TraceSource.TraceEvent call will automatically check the level of the message (TraceEventType.Information) against the configured level of the associated Switch and will determine whether or not the message should actually be written out.
通过使用不同的命名TraceSource为每个类型,你可以从这些类单独控制记录。你可以启用MyClass1的日志记录,或者你可以禁用它,或者你可以启用它,但都将其记录只有当消息(TraceEventType)的水平大于一定值(也许仅记录警告和更高版本)。同时,你可以把记录在MyClass2的开启或关闭,或设置为一个级别,完全独立MyClass1的了。在app.config文件中所有这一切都使能/禁止/层次的东西发生。
By using a differently named TraceSource for each type, you can control the logging from those classes individually. You could enable MyClass1 logging or you could disable it or you could enable it but have it log only if the level of the message (TraceEventType) is greater than a certain value (maybe only log "Warning" and higher). At the same time, you could turn logging in MyClass2 on or off or set to a level, completely independently of MyClass1. All of this enabling/disabling/level stuff happens in the app.config file.
使用app.config文件,你也可以控制以同样的方式全部TraceSources(或TraceSources组)。所以,你可以配置让MyClass1的和MyClass2的都是由同一开关控制。
Using the app.config file, you could also control all TraceSources (or groups of TraceSources) in the same way. So, you could configure so that MyClass1 and MyClass2 are both controlled by the same Switch.
如果您不希望为每个类型不同名称的TraceSource,你可以只创建同一TraceSource在每一个类:
If you don't want to have a differently named TraceSource for each type, you could just create the same TraceSource in every class:
public class MyClass1
{
private static readonly TraceSource ts = new TraceSource("MyApplication");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
public class MyClass2
{
private static readonly TraceSource ts = new TraceSource("MyApplication");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
这种方式,你可以让你的应用程序中的所有记录发生在同一级别(或关闭,或走一样的TraceListener,或其他)。
This way, you could make all logging within your application happen at the same level (or be turned off or go the same TraceListener, or whatever).
您也可以配置应用程序的不同部分是独立配置的,而不必去了麻烦,在每种类型定义一个独特的TraceSource的:
You could also configure different parts of your application to be independently configurable without having to go the "trouble" of defining a unique TraceSource in each type:
public class Analysis1
{
private static readonly TraceSource ts = new TraceSource("MyApplication.Analysis");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
public class Analysis2
{
private static readonly TraceSource ts = new TraceSource("MyApplication.Analysis");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
public class DataAccess1
{
private static readonly TraceSource ts = new TraceSource("MyApplication.DataAccess");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
public class DataAccess2
{
private static readonly TraceSource ts = new TraceSource("MyApplication.DataAccess");
public DoSomething(int x)
{
ts.TraceEvent(TraceEventType.Information, "In DoSomething. x = {0}", x);
}
}
使用类仪器这种方式,你可以在一个级别使您的应用程序日志中的数据访问的一部分,而分析你的应用程序日志等级不同的应用程序的两个部分的一部分(当然,任何一个或可以进行配置,以便记录被禁止)。
With your class instrumented this way, you could make the "DataAccess" part of your app log at one level while the "Analysis" part of your app logs at a different level (of course, either or both parts of your app could be configured so that logging is disabled).
下面是一个app.config文件配置TraceSources和TraceSwitches的一部分:
Here is a part of an app.config file that configures TraceSources and TraceSwitches:
<system.diagnostics>
<trace autoflush="true"></trace>
<sources>
<source name="MyClass1" switchName="switch1">
<listeners>
<remove name="Default"></remove>
<add name="console"></add>
</listeners>
</source>
<source name="MyClass2" switchName="switch2">
<listeners>
<remove name="Default"></remove>
<add name="console"></add>
</listeners>
</source>
</sources>
<switches>
<add name="switch1" value="Information"/>
<add name="switch2" value="Warning"/>
</switches>
<sharedListeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener">
</add>
<add name="file"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.txt">
</add>
</sharedListeners>
</system.diagnostics>
正如你所看到的,你可以配置一个TraceSource和单一的交换机和所有记录将与控制的单级发生(例如,你可以把所有注销或使其记录在一定的水平)。
As you can see, you could configure a single TraceSource and a single Switch and all logging would occur with a single level of control (i.e. you could turn all logging off or make it log at a certain level).
另外,你可以定义多个TraceSources(并参考相应的TraceSources在code)以及多个开关。该交换机可以是共享(即多个TraceSources可以使用同一个开关)。
Alternatively, you could define multiple TraceSources (and reference the corresponding TraceSources in your code) and multiple Switches. The Switches may be shared (i.e. multiple TraceSources can use the same Switch).
最后,通过投入多一点的努力,现在在你的应用程序中使用TraceSources和引用适当命名的TraceSources在code(即逻辑上定义的TraceSource的名称,以便您可以控制的期望程度超过记录),您将获得显著的灵活性,从长远来看。
Ultimately, by putting in a little more effort now to use TraceSources and to reference appropriately named TraceSources in your code (i.e. define the TraceSource names logically so that you can have the desired degree of control over logging in your app), you will gain significant flexibility in the long run.
下面是一些链接,可以帮助你System.Diagnostics程序作为你前进:
Here are a few links that might help you with System.Diagnostics as you go forward:
[VB.net]什么是日志记录的最佳方法?
Does在净TraceSource / TraceListener的框架有类似的log4net的格式化程序的东西吗?
在我张贴的联系,往往存在最好的日志框架的讨论。我不是想说服你从System.Diagnostics程序改变。该链接也往往有关于使用System.Diagnostics程序良好的信息,这就是为什么我张贴了他们。
In the links I posted, there is often discussion of the "best" logging framework. I am not trying to convince you to change from System.Diagnostics. The links also tend to have good information about using System.Diagnostics, that is why I posted them.
几个我张贴的链接包含指向 Ukadc.Diagnostics 。这是一个非常酷的添加对库System.Diagnostics程序,增加了丰富的格式化功能,类似于你可以用log4net的和NLOG做。这个库强加给你的应用程序中的配置,唯一的依赖,而不是一个code或引用依赖。
Several of the links I posted contain a link to Ukadc.Diagnostics. This is a really cool add on library for System.Diagnostics that adds rich formatting capability, similar to what you can do with log4net and NLog. This library imposes a config-only dependency on your app, not a code or reference dependency.
这篇关于谈到通过的app.config跟踪功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!