【发布时间】:2020-10-18 06:08:14
【问题描述】:
我正在使用 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"而不是xsi:type="Database"
标签: mongodb asp.net-core nlog