C# 用于我的自定义事件日志条目的事件 ID 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1755615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:32:47  来源:igfitidea点击:

What event id to use for my custom event log entries?

c#windowsevent-log

提问by Niran

Is there any ranges of valid event IDs which should be used by custom applications while logging to Windows EventLog? Or I can use any event ID of my choice (1,2,3,4....). P.S, I am developing in C#.NET.

是否有任何范围的有效事件 ID 应由自定义应用程序在登录到 Windows EventLog 时使用?或者我可以使用我选择的任何事件 ID(1、2、3、4....)。PS,我正在用 C#.NET 开发。

回答by Martin

EventIds are application specific so you can use whatever ranges you like. Just ensure you document what you have used and where so that you can ensure you don't use an id twice, or to facilitate easier debugging.

EventIds 是特定于应用程序的,因此您可以使用您喜欢的任何范围。只需确保记录使用的内容和位置,以确保不会重复使用 id,或方便调试。

But keep in mind...

但请记住...

Like when Henry Ford said "you can have any color you want as long as it's black" - you can also use whatever range you like as long as that range falls inside the range of 0 and 65535.

就像亨利福特所说的“只要是黑色,你就可以拥有任何你想要的颜色”——你也可以使用任何你喜欢的范围,只要该范围落在 0 和 65535 的范围内。

回答by colbybhearn

Sure enough, it is up to the author to define and track event IDs they use and what they mean.

果然,由作者定义和跟踪他们使用的事件 ID 及其含义。

Here is a reference: http://msdn.microsoft.com/en-us/library/e29k5ebc.aspx- Particularly interesting is the part about not writing messages with IPv6 addresses (because of the %character) to the event log. I bet you can use a parameter to work around that though.

这是一个参考:http: //msdn.microsoft.com/en-us/library/e29k5ebc.aspx- 特别有趣的是关于不将带有 IPv6 地址的消息(因为%字符)写入事件日志的部分。我打赌你可以使用一个参数来解决这个问题。

回答by MrHIDEn

Edit1: I tested that and it is not true that eventID is 32bits. It is only 16 bits.

编辑 1:我对此进行了测试,但 eventID 不是 32 位。它只有 16 位。

eventId is Int32, from -2,147,483,648 to 2,147,483,647

eventId 是 Int32,从 -2,147,483,648 到 2,147,483,647

EventLog.WriteEntry Method (String, String, EventLogEntryType, Int32)

public static void WriteEntry(
    string source,
    string message,
    EventLogEntryType type,
    int eventID
)

回答by Vinod Srivastav

Technically you can use any values between 1 - 65536for that.

从技术上讲,您可以为此使用1 - 65536之间的任何值。

But if you are someone who writes tons of verbose log like me you will find it difficult to relate a bunch of entries together then I would suggest to generate a random unique value every time the code executes with this you can identify the events, even the much better idea would be to create your own log & source to use this instead of writing everything in the Application log. like

但是,如果您是像我一样编写大量冗长日志的人,您会发现很难将一堆条目关联在一起,那么我建议每次执行代码时都生成一个随机的唯一值,这样您就可以识别事件,甚至更好的主意是创建自己的日志和源来使用它,而不是在应用程序日志中写入所有内容。喜欢

 Random rnd = new Random();
 EventId = rnd.Next(0, 65535);

回答by JRV

The hi bits of the ID are reserved for testing, debug and other flags used for development. The usable bits are:

ID 的高位保留用于测试、调试和其他用于开发的标志。可用的位是:

0x0000 - 0xffff

0x0000 - 0xffff

See: Event Message Structure

请参阅:事件消息结构

The upper bits should be avoided but all values for the bottom bits are available if you create a custom source. If you use a system or pre-existing source you will collide and likely get the wrong message. Messages are taken from the registered sources message DLL file. A custom message file can be built using the message file compiler from the SDK.

应避免使用高位,但如果您创建自定义源,则低位的所有值都可用。如果您使用系统或预先存在的源,您将发生冲突并可能收到错误消息。消息取自已注册的源消息 DLL 文件。可以使用 SDK 中的消息文件编译器构建自定义消息文件。