我可以添加新版本的事件方法并使用相同的事件 ID 吗?

Can I add a new version of and event method and use same event ID?

关于版本控制,documentation for the Semantic Logging Application Block 建议:

If you do need to modify your EventSource class, you should restrict your changes to adding methods to support new log messages, and adding overloads of existing methods (that would have a new event ID). You should not delete or change the signature of existing methods.

假设我有以下 EventSource class:

[EventSource(Name = "Instrumentation")]
public class InstrumentationEventSource : EventSource {
    private static readonly Lazy<InstrumentationEventSource> Singleton = new Lazy<InstrumentationEventSource>(() => new InstrumentationEventSource());

    public static InstrumentationEventSource Log { get { return Singleton.Value; } }

    private InstrumentationEventSource() {}

    [Event(eventId: 901)]
    public void EndPage(string url) {
        WriteEvent(901, url);
    }
}

然后我想添加对记录查询字符串的支持。我可以添加具有相同 ID 的重载方法吗?

[Event(eventId: 901)]
public void EndPage(string url) {
    WriteEvent(901, url);
}

[Event(eventId: 901)]
public void EndPage(string url, string queryString) {
    WriteEvent(901, url, queryString);
}

如何在对应用程序或进程外主机日志记录应用程序的影响尽可能小的情况下支持未来的修改?

我可以通过在模型中添加内容来简化签名吗class?

public class LogData {
    public string url { get; set; }
    // public string queryString { get; set; }
}

[Event(eventId: 901)]
public void EndPage(LogData data) {
    WriteEvent(901, data);
    // Or does the params object[] args parameter not support classes?
    // WriteEvent(901, data.url);
    // And this would have to be changed anyway?
    // WriteEvent(901, data.url, data.queryString);
}

我不太确定事件 ID 适合这一切,以及 EventSource class 的维护需要对此多加注意。

EventSource 将您的日志记录显示为结构化数据。从这个意义上讲,它是正式发布的 API.

And then I wanted to add support for logging a query string. Could I add an overloaded method with the same ID?

没有。 EventId 必须是唯一的。但是,如果您只需要向该方法添加新参数,那么只要将它们添加到允许的签名末尾即可(并修改事件版本)。

Could I keep the signatures simpler by making the additions in a model class?

否,因为事件参数必须是原始类型(基本上)。不支持自定义类型,例如 LogData。

假设你有:

[Event(eventId: 901)]
public void EndPage(string url) {
    WriteEvent(901, url);
}

如果您想向该方法添加更多信息,那么您可以修改现有方法:

[Event(eventId: 901, Version = 1)]
public void EndPage(string url, string queryString) 
{
    WriteEvent(901, url, queryString);
}

如果您不希望这影响现有的呼叫者,您可以为参数指定一个默认值:

[Event(eventId: 901, Version = 1)]
public void EndPage(string url, string queryString = null) 
{
    WriteEvent(901, url, queryString);
}

或者您可以提供非事件重载供现有客户端使用:

[NonEvent]
public void EndPage(string url) {
    this.EndPage(url, null);
}

[Event(eventId: 901, Version = 1)]
public void EndPage(string url, string queryString) 
{
    WriteEvent(901, url, queryString);
}