A Quick Note
I know the question title is a little odd but I haven't had much caffeine this morning so I'm having trouble thinking clearly right now. If you have any suggestions for a better title, please comment below and I'll update.
The Problem
I've built a custom logging solution that allows us (my organization) to log events within our applications. The solution requires an event type when logging an event and I'd like to add support for application specific event types.
We currently have a single enum
that contains all of our event types, and I believe this is not the correct way to approach it since adding specific event types related to each application will increase the number of values available in the enum
by a lot. For example, with support added for only two applications, we're already up to 22 different event types.
My Current Thoughts
I've thought about creating a single class that contains multiple enum
declarations for the different event types, but this will not work since I can't create a single method that will handle all the different enum
s, and I do not want to create multiple methods for each different enum
since that will be a maintenance nightmare.
// Simple example.
public bool LogEvent(EventType type) { /* ... */ }
A possible solution would be to create a collection of classes with several const
declarations like so:
public sealed class EventTypes {
public class EventType {
public string Name { get; private set; }
public EventType(string name) { Name = name; }
}
public class Validation : EventType {
public const int GeneralValidation = 100;
public const int DataValidation = 101;
public const int UserValidation = 102;
private Validation() : base("Validation") { }
}
public class Application1 : EventType {
public const int Event1 = 1000;
public const int Event2 = 1001;
private Application1() : base("Application1") { }
}
}
However, I'm not so sure that this is an approach that would follow best practices in an object oriented environment and feels kind of "hacky". I would also want to prevent the classes from being instantiated outside of the EventTypes
container class (would abstract
still allow the private constructor to run?), and EventTypes
should not be inheritable, so sealed
could apply there, but I'm not sure on that either.
Requirements
The solution I'm looking for will allow me to have a single method for logging events as I currently have. An example implementation when calling the LogEvent
method would look like this:
LogEvent(EventTypes.ApplicationName.EventTypeName);
LogEvent(EventTypes.Validation.DataValidation);
The main goal is to enforce consistency in our data so that we ensure no records are lost in queries and views. The primary thing I want to avoid is having developers enter an application name manually because this can lead to inconsistency, for example:
LogEvent("Application Name", "Event Name");
LogEvent("App Name", "Evt Name");
Sure, I can use constants to prevent this, but that would require the developers to use them, and requires code reviews to ensure they are following the standard. The ideal solution would cause compilation failure if an invalid type was supplied.
The Question
This probably isn't the best phrasing; however, what options are available to replace a single enum
with a solution that supports multiple types and follows best practices in an object oriented environment?
IEvent
interface, and have each application provide its own events.LogEvent
could then take anIEvent
and call a method on it to do the actual logging (you could pass the a generic "log writer" to the event itself). "I would also want to prevent the classes from being instantiated outside of the EventTypes container class" - why (why is that important)? Let each application provide its own set of events.