如何指定Serial.Sinks.MongDB接收器的集合



github上的文档使用以下连接字符串:

.WriteTo.MongoDB("mongo://mymongodb/log")

它说,为了使这个例子发挥作用,您需要向服务器添加一个名为log的集合。但是,此连接字符串会将您连接到服务器上的日志数据库,而不会指定任何集合。我在连接字符串中找不到任何关于指定集合的信息(这很有意义(,那么我如何告诉MongoDB接收器要向哪个集合写入呢?

我在应用程序中添加了SelfLog.Out=Console.Out,但在Output窗口中看不到任何有助于我的内容。

后来:我在数据库中添加了一个名为"log"的集合,Serilog正在写入该集合。因此,"日志"似乎已烘焙到驱动程序中。我想要这个特定数据库的两个不同的日志。所以我的问题仍然存在。有办法做到这一点吗?

下载源代码,并在MongoDB LoggerConfiguration类构造函数的签名中添加一个集合参数。更改后的代码标有**。

public static LoggerConfiguration MongoDB(
    this LoggerSinkConfiguration loggerConfiguration,
    string databaseUrl,
    **string collectionName = null,**
    LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
    int batchPostingLimit = MongoDBSink.DefaultBatchPostingLimit,
    TimeSpan? period = null,
    IFormatProvider formatProvider = null)
{
    if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration");
    if (databaseUrl == null) throw new ArgumentNullException("databaseUrl");
    var defaultedPeriod = period ?? MongoDBSink.DefaultPeriod;
    return loggerConfiguration.Sink(
        new MongoDBSink(
            databaseUrl,
            batchPostingLimit,
            defaultedPeriod,
            formatProvider,
            **collectionName ??** MongoDBSink.DefaultCollectionName,
            new CreateCollectionOptions()),
        restrictedToMinimumLevel);
}

最新更新