Answer a question

I'm using Nlog(4.7.5), Nlog.mongo(4.6.0.118) and mongoDB.Driver(2.11.3). when logging with mongodb I need to create collection like this "log-2020-10-18". so the 18th date logs are store in this collection. The next day or etc. logs will store in "log-${shortdate}" collection. everyday the logs store in the format of collection name. in short I want to store logs daywise in mongodb.so it will easy to see day to day logs. can I create collection at runtime(I mean in nlog.config file) so is that possible to do that? how?

here is the mongodb target

    <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 logs

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

Answers

Have you tried this (Notice the use of 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>

Make sure to use https://www.nuget.org/packages/NLog.Mongo/4.6.0.123 (or newer) for improved support of using ${shortdate} in collectionName=

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐