Nlog -Mongodb创建集合

问题描述

我正在使用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,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,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,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(或更高版本)以更好地支持在${shortdate}中使用collectionName=