I'm trying to figure out how to manage my event ids. Up to this point I've been putting each event id in each method manually with each step in a method numbered sequentially. This doesn't allow me to effectively filter events in the event log. In order to use the filter in the event log, it seems that every logged event must have its own unique id.
I could store them all in a table with the description linked to them, but then as my code executes, I'm logging "magic" meaningless event codes.
I did a Google search, but I appear to be at a loss as to the correct keywords to use to get to the bottom of this issue.
Thanks in advance
Windows uses the following levels: Critical, Error, Warning, Information, Verbose (although software developers may extend this set and add own specific levels).
Event identifiers uniquely identify a particular event. Each event source can define its own numbered events and the description strings to which they are mapped in its message file. Event viewers can present these strings to the user.
Event ID - 104This event is logged when the log file was cleared. Resolution. This is a normal condition. No further action is required.
Like Ben's suggestion, it's probably worth using a level of indirection - but instead of using an int for the code, I'd use an actual enum, so for Ben's example:
public enum EventId
{
[Format("Building command object from {0}.")]
BuildingCommandObject = 1,
[Format("Command object build successfully.")]
CommandObjectBuilt = 2,
[Format("Connecting to {0}.")]
ConnectingToDatabase = 3,
[Format("Executing command against database {0}.")]
ExecutingCommand = 4,
[Format("Command executed successfully.")]
CommandExecuted = 5,
[Format("Disconnecting from {0}.")]
DisconnectingFromDatabase = 6,
[Format("Connection terminated")]
Disconnected = 7
}
Or alternatively (and in a more object-oriented fashion) use the "smart enum" pattern):
public class LogEvent
{
public static readonly LogEvent BuildingCommandObject = new LogEvent(1,
"Building command object from {0}");
// etc
private readonly int id;
private readonly string format;
// Add the description if you want
private LogEvent(int id, string format)
{
this.id = id;
this.format = format;
}
public void Log(params object[] data)
{
string message = string.Format(format, data);
// Do the logging here
}
}
You can then call:
LogEvent.BuildingCommandObject.Log("stuff");
With a bit of work you may be able to expose this in a safe way with different log events having a different interface to make it safe (at compile-time) in terms of how many parameters each one has. In fact I'm sure you could do it using interfaces and a private nested class, but it's 4am and I'm too tired to write it out atm :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With