净:如何晚饭preSS TraceSource头(QUOT; SOURCENAME TraceEventType:编号:")?晚饭、编号、preSS、QUOT

2023-09-04 08:36:43 作者:十里盛世,灯火阑珊

我有,我用它来记录一个VB.Net应用程序的初始化TraceSource对象。它有几个TraceListeners附:

I have a TraceSource object that I use to log the initialization of a VB.Net application. It has several TraceListeners attached:

ConsoleTraceListener TextWriterTraceListener会 EventLogTraceListener

对于前两种我想进入输出为原始 - 也就是,没有的标准头:

For the first two I want the entry output to be "raw" - that is, without the standard header:

SOURCENAME TraceEventType:ID:

我实现了一个包装,这是否当TraceEventType设置为详细:

I have implemented a wrapper that does this when the TraceEventType is set to Verbose:

If _buffer.EventType = TraceEventType.Verbose Then
    For Each listener As TraceListener In _traceSource.Listeners
        listener.Write(_buffer.Text)
    Next
Else
    _traceSource.TraceEvent(_buffer.EventType, id, _buffer.Text)
End If

我能做到这一点对所有的追踪,但随后在事件日志中的所有条目将被列出级别=信息。所以,我希望能够到指定跟踪消息的严重性,但我找不到在TraceSource或允许我做这件事的TraceListeners任何方法。据我所知,有的TraceListener这些选项写它:

I could do this for all the tracing, but then all entries in the EventLog would be listed with Level = Information. So I want to be able to specify the severity of the trace message, but I can't find any method on the TraceSource or the TraceListeners that allows me to do this. As far as I can tell, TraceListener has these options for writing to it:

写() 的WriteLine() TRACEDATA() TraceEvent() TraceTransfer()

最后3允许提供一个TraceEventType(其中正确标签的事件日志条目,但结果输出到控制台和日志文件,然后包括prefixes并最终像这样(例如):

The last 3 allows for providing a TraceEventType (which correctly labels the EventLog entries, but the resulting output to the console and the log file then includes the prefixes and ends up like this (for example):

引导程序警告:0:无法验证程序集

有没有办法覆盖ConsoleTraceListener和TextWriterTraceListener会如何格式化它们的输出不包括此标题,而在同一时间能够与一个TraceEventType(对于事件日志)标签中的条目?

Is there a way to override how the ConsoleTraceListener and TextWriterTraceListener format their output to not include this header, while at the same time being able to tag the entries with a TraceEventType (for the EventLog)?

这是最好的,我已经提出了至今:

This is the best I have come up with so far:

For Each listener As TraceListener In _traceSource.Listeners
    If listener.GetType Is GetType(ConsoleTraceListener) OrElse listener.GetType Is GetType(TextWriterTraceListener) Then
        listener.Write(_buffer.Text)
    Else
        listener.TraceEvent(Nothing, _traceSource.Name, _buffer.EventType, id, _buffer.Text)
    End If
Next

这似乎工作,但对 TraceListener.TraceEvent方法来自微软的文档中,它说:

This seems to work, but in the documentation on the TraceListener.TraceEvent Method from Microsoft, it says:

重要提示:此方法不适合直接调用应用程序code,但在调试,跟踪的成员,和TraceSource类来跟踪数据写入输出

..所以我不知道,如果它是一个很好的事是什么?

..so I'm not sure if it's a good thing to do?

编辑:

我刚刚意识到,如果我做这样的事情我最后一个例子在这里,我并不需要TraceSource可言,因为它正在反正绕过。但是,这也意味着我要实现我自己的过滤和交换机制(但是这也许是一个确定的价格支付得到它的工作就是我想要的)。

I just realized that if I do something like my last example here, I don't need the TraceSource at all, since it's being bypassed anyway. But it also means I have to implement my own filtering and switching mechanisms (but that is maybe an OK price to pay to get it to work the way I want).

推荐答案

这是有,你可以使用formattable听众另一个类似项目的基本诊断的,它实际上最初是由Ukadc.Diagnostics启发。

Another similar project that has formattable listeners that you could use is Essential Diagnostics, which was actually originally inspired by Ukadc.Diagnostics.

您已经表明,但是,你不希望外部依赖,但你仍然有几个选项,而不框架重写部分:

You have indicated, however, that you don't want external dependencies, but you still have several options without re-writing parts of the Framework:

(一) 而不是改写TraceSource,在.NET框架的设计扩展点是写你自己的TraceListener。

(A) Rather than rewrite TraceSource, the designed extension point in the .NET Framework is to write your own TraceListener.

如果你写你自己的跟踪监听器ConsoleWithout prefixListener和FileWithout prefixListener,那么你就可以覆盖TraceEvent()方法只是转发邮件TraceWrite()(并删除prefixes)。

If you write your own trace listeners "ConsoleWithoutPrefixListener" and "FileWithoutPrefixListener", then you can override the TraceEvent() methods to just forward the message to TraceWrite() (and drop the prefixes).

其实既不ConsoleTraceListener或TextWriterTraceListener会是密封的,所以我觉得你可以继承他们,让这个正在与TraceEvent的一条线覆盖()方法(加上构造函数)。

In fact neither ConsoleTraceListener or TextWriterTraceListener are sealed, so I think you could inherit from them and get this working with a one line override of the TraceEvent() method (plus the constructor).

(B)的 另一种办法是离开EventLogTraceListener可配置对源代码,但配置下(而不是跟踪源)另外两个监听器。

(B) Another alternative would be to leave EventLogTraceListener configurated against the source but configure the other two listeners under (rather than trace source).

这样做的缺点是在code您需要在每次登录两次,如:

The drawback of this is that in your code you need to log twice every time, e.g.:

_traceSource.TraceEvent(_buffer.EventType,ID,_buffer.Text)   Trace.TraceWrite(_buffer.Text)

_traceSource.TraceEvent(_buffer.EventType, id, _buffer.Text) Trace.TraceWrite(_buffer.Text)

如果你想要写有prefixes一些消息,有些没有,那么你将需要两个跟踪源:一个是配置有三个监听器,和一个只与事件日志监听器

If you want to write some messages with prefixes and some without then you will need two trace sources: one that is configured with all three listeners, and one with only the event log listener.

然后,在你的包装或者写源的(三个)或源B +跟踪静态方法。

Then, in your wrapper either write to source A (all three) or source B + Trace static methods.

(三) 就个人而言,我的指导,将不使用跟踪写入事件日志 - 如果问题是重要的,足以写入事件日志中,您通常不希望用户能够通过配置来关闭它们。

(C) Personally, my guidance would be not to use tracing for writing to the event log -- if issues are important enough to write to the event log you generally don't want the user to be able to turn them off via configuration.

在这种情况下,你写的包装直接向事件日志(EventLog.WriteEntry或其他),然后你的code写入跟踪源和/或跟踪静态方法的文件和控制台。

In this case your wrapper writes to the event log directly (EventLog.WriteEntry or whatever) and then your code writes to the trace source and/or Trace static methods for the file and console.

请注意,要想让写入事件日志,你需要考虑的权限正常工作。要创建你需要的事件日志源以管理员身份运行的。作为一名开发人员,你可能通常有管理员的权限,所以你需要的人谁不的情况下适当地进行测试。

Note that to get writing to Event Log working correctly you need to take into account permissions. To create an event log source you need to be running as administrator. As a developer you probably normally have admin permissions, so you need to properly test this in the context of someone who doesn't.

另外请注意,这仅仅是需要管理员权限的初始创建,这是当你写的第一封邮件自动完成的,因此,如果你已经做了作为一个开发管理员,你需要找到一个干净的机器上测试

Also note that it is only the initial creation that needs admin permissions, and this is automatically done when you write the first message, so if you have already done it as a developer admin you need to find a clean machine to test on.

这一点,因为,通常你需要有一个EventLogInstaller为您的code,这是由InstallUtil(或同等MSI或其他)运行的一部分,创建事件日志源在安装过程中(因为安装的是的由管理员来完成)。然后,当程序运行时的源存在。

Because of this, normally you need to have an EventLogInstaller as part of your code, which is run by InstallUtil (or equivalent MSI or whatever) that creates the event log source during install (because install is done by an administrator). Then, when the program runs the source exists.

那么,这是什么都做书面痕迹 - 好吧,如果你做的唯一一件事就是配置您的配置的EventLogTraceListener那么对于普通用户将无法正常工作;它会尝试将事件写入源(在initializeData属性),这将尝试创建源,如果没有运行的管理将会失败。

So, what does this have to do with writing to traces -- well, if the only thing you do is configure the EventLogTraceListener in your config then for normal users it won't work; it will try and write events to the source (in the initializeData attribute), which will then try to create source and if not running as admin will fail.

如果您添加一个安装程序的事件来源,那么你仍然有一个问题,如果有人更改配置文件。

If you do add an installer for the event source, then you still have a problem if someone changes the config file.

这一点,因为,我建议,无论是EventLogInstaller和事件日志可以直接在code创建的,以确保名称匹配,而不是通过跟踪基础设施。

Because of this, I would recommend that both the EventLogInstaller and EventLog are created directly in code, to ensure the names match, and not go through the tracing infrastructure.