首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nlog -Mongodb创建集合

Nlog -Mongodb创建集合
EN

Stack Overflow用户
提问于 2020-10-18 06:08:14
回答 1查看 231关注 0票数 2

我使用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的目标

代码语言:javascript
复制
    <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日志

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-18 08:38:11

您是否尝试过这种方法(注意xsi:type="Mongo"的使用):

代码语言:javascript
复制
    <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}的支持

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64410247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档