Nlog -Mongodb 创建集合

Nlog -Mongodb create collection

我正在使用 Nlog(4.7.5)、Nlog.mongo(4.6.0.118) 和 mongoDB.Driver(2.11.3)。 使用 mongodb 登录时,我需要创建这样的集合 "log-2020-10-18"。 所以第 18 天的日志存储在这个集合中。第二天等日志将存储在"log-${shortdate}"集合中。每天日志以集合名称的格式存储。 简而言之,我想每天在 mongodb.so 中存储日志,这样很容易看到日常日志。 我可以在运行时创建集合吗(我的意思是在 nlog.config 文件中) 那有可能这样做吗?怎么样?

这里是mongodb目标

    <target xsi:type="Database"
                    name="mongo"
                    includeDefaults="false"
                    connectionString="mongodb://localhost/financeLogs"
                    collectionName="log-${shortdate}"
                    databaseName="financeLogs">
                <property name="LongDate" layout="${longdate}" bsonType="DateTime" />
                <property name="Level" layout="${level}"/>
                <property name="Message" layout="${message}"/>
    
            </target>

InternalLogger 日志

2020-10-18 14:26:06.1806 Warn Error when setting 'log-${shortdate}' on attibute 'collectionName' Exception: System.NotSupportedException: Parameter collectionName not supported on DatabaseTarget
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   at NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
2020-10-18 14:26:06.2640 Error Exception when parsing D:\Git\finance.api\FinanceAPI\bin\Debug\netcoreapp3.1\nlog.Development.config.  Exception: System.NotSupportedException: Parameter collectionName not supported on DatabaseTarget
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   at NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   at NLog.Config.LoggingConfigurationParser.ParseTargetElement(Target target, ILoggingConfigurationElement targetElement, Dictionary`2 typeNameToDefaultTargetParameters)
   at NLog.Config.LoggingConfigurationParser.ParseTargetsElement(ILoggingConfigurationElement targetsElement)
   at NLog.Config.LoggingConfigurationParser.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.LoggingConfigurationParser.LoadConfig(ILoggingConfigurationElement nlogConfig, String basePath)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(ILoggingConfigurationElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
2020-10-18 14:26:06.2980 Warn Error has been raised. Exception: NLog.NLogConfigurationException: Exception when parsing D:\Git\finance.api\FinanceAPI\bin\Debug\netcoreapp3.1\nlog.Development.config. 
 ---> System.NotSupportedException: Parameter collectionName not supported on DatabaseTarget
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   at NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   at NLog.Config.LoggingConfigurationParser.ParseTargetElement(Target target, ILoggingConfigurationElement targetElement, Dictionary`2 typeNameToDefaultTargetParameters)
   at NLog.Config.LoggingConfigurationParser.ParseTargetsElement(ILoggingConfigurationElement targetsElement)
   at NLog.Config.LoggingConfigurationParser.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.LoggingConfigurationParser.LoadConfig(ILoggingConfigurationElement nlogConfig, String basePath)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(ILoggingConfigurationElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---
2020-10-18 14:26:06.3558 Info Validating config: TargetNames=file, fileAsException, ConfigItems=56, FilePath=D:\Git\finance.api\FinanceAPI\bin\Debug\netcoreapp3.1\nlog.Development.config
2020-10-18 14:26:06.3859 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: file
2020-10-18 14:26:06.3975 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: fileAsException

你试过了吗(注意xsi:type="Mongo"的用法):

    <target xsi:type="Mongo"
            name="mongo"
            includeDefaults="false"
            connectionString="mongodb://localhost/financeLogs"
            collectionName="log-${shortdate}"
            databaseName="financeLogs">
        <property name="LongDate" layout="${longdate}" bsonType="DateTime" />
        <property name="Level" layout="${level}"/>
        <property name="Message" layout="${message}"/>
    </target>

确保使用 https://www.nuget.org/packages/NLog.Mongo/4.6.0.123(或更新版本)以改进对在 collectionName=

中使用 ${shortdate} 的支持