suy nghĩ đầu tiên - và tôi đã không hoàn toàn nghĩ này thông qua, nhưng nó có vẻ như một khả năng hợp lý:
public class LogEvent
{
/* This is the event code you reference from your code
* so you're not working with magic numbers. It will work
* much like an enum */
public string Code;
/* This is the event id that's published to the event log
* to allow simple filtering for specific events */
public int Id;
/* This is a predefined string format that allows insertion
* of variables so you can have a descriptive text template. */
public string DisplayFormat;
/* A constructor to allow you to add items to a collection in
* a single line of code */
public LogEvent(int id, string code, string displayFormat)
{
Code = code;
Id = id;
DisplayFormat = displayFormat;
}
public LogEvent(int id, string code)
: this(id, code, null)
{
}
public LogEvent()
{
}
}
Sau đó bạn có thể có một lớp quản lý sự kiện mà kết thúc tốt đẹp danh sách các sự kiện cung cấp một phương pháp mà truy vấn danh sách theo tham số bạn vượt qua - ví dụ:
public class EventManager
{
private List<LogEvent> _eventList;
public LogEvent this[string eventCode]
{
get
{
return _eventList.Where(i => i.Code.Equals(eventCode)).SingleOrDefault();
}
}
public LogEvent this[int id]
{
get
{
return _eventList.Where(i => i.Id.Equals(id)).SingleOrDefault();
}
}
public void AddRange(params LogEvent[] logEvents)
{
Array.ForEach(logEvents, AddEvent);
}
public void Add(int id, string code)
{
AddEvent(new LogEvent(id, code));
}
public void Add(int id, string code, string displayFormat)
{
AddEvent(new LogEvent(id, code, displayFormat));
}
public void Add(LogEvent logEvent)
{
_events.Add(logEvent);
}
public void Remove(int id)
{
_eventList.Remove(_eventList.Where(i => i.id.Equals(id)).SingleOrDefault());
}
public void Remove(string code)
{
_eventList.Remove(_eventList.Where(i => i.Code.Equals(code)).SingleOrDefault());
}
public void Remove(LogEvent logEvent)
{
_eventList.Remove(logEvent);
}
}
này cho phép quản lý đơn giản của ev ent định nghĩa có thể được quản lý độc lập cho mỗi TraceSource.
var Events = new EventManager();
Events.AddRange(
new LogEvent(1, "BuildingCommandObject", "Building command object from {0}."),
new LogEvent(2, "CommandObjectBuilt", "Command object built successfully."),
new LogEvent(3, "ConnectingToDatabase", "Connecting to {0}."),
new LogEvent(4, "ExecutingCommand", "Executing command against database {0}".),
new LogEvent(5, "CommandExecuted", "Command executed succesfully."),
new LogEvent(6, "DisconnectingFromDatabase", "Disconnecting from {0}."),
new LogEvent(7, "Disconnected", "Connection terminated.")
)
Và bạn có thể truy cập vào các sự kiện bằng cách sử dụng định danh có ý nghĩa bạn đã gán:
var evt = Events["ConnectingToDatabase"];
TraceSource.TraceEvent(TraceEventType.Information, evt.Id, evt.DisplayFormat, otherParams);
hoặc
var evt = Events[1024];
Console.WriteLine("Id: {1}{0}Code: {2}{0}DisplayFormat{3}",
Environment.NewLine, evt.Id, evt.Code, evt.DisplayFormat);
này có lẽ sẽ đơn giản hóa quản lý sự kiện của bạn, bạn không còn kêu gọi của bạn các sự kiện bằng số ảo, thật đơn giản để quản lý tất cả các sự kiện của bạn ở một nơi - lớp EventManager của bạn và bạn vẫn có thể lọc nhật ký sự kiện của mình theo số ma thuật mà nó yêu cầu bạn lọc theo.
Không bao giờ sử dụng id sự kiện khi đăng nhập vào nhật ký sự kiện. Tôi chỉ cần đặt đủ thông tin về những gì đã xảy ra tại thời điểm đó và để cô ấy rip ... – Will
@Will - Vậy làm thế nào để bạn lọc nhật ký sự kiện của mình khi bạn đang cố gắng tìm các mục có liên quan? Ví dụ, nếu tôi có một thành phần hiện đang ghi nhật ký một cách chi tiết nhưng sau một sự kiện cụ thể - cách duy nhất tôi có thể lọc trên đó là lọc trên id sự kiện. – BobTheBuilder
@Will - ý tưởng ở đây là để đánh giá tần suất xuất hiện của một sự kiện cụ thể mà không phải đào qua mục nhật ký theo mục để tìm các mục có liên quan. – BobTheBuilder