nlog 相关问题

NLog是.NET的免费日志记录平台

如何让NLog停止写入冗余信息?

我的Programm.cs: var 记录器 = LogManager.Setup() .RegisterNLogWeb() .LoadConfigurationFromFile("nlog.config") .GetCurrentClassLogger(); 尝试 { var builder = WebApplication.

回答 1 投票 0

NLog 仅记录到一个 .log 文件

我正在使用 NLog.Web.AspNetCore 来生成日志,但 Nlog 仅记录到一个所有带有日期的 .log 文件(nlog.config 中的所有文件名)。 这是我的 nlog.config 文件 我正在使用 NLog.Web.AspNetCore 来生成日志,但 Nlog 仅记录到一个包含日期的 .log 文件(nlog.config 中的所有文件名)。 这是我的 nlog.config 文件 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Info" internalLogFile="c:\temp\internal-nlog-AspNetCore.txt"> <!-- enable asp.net core layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="request-time" fileName="(absolute path)\Log\request-time.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <target xsi:type="File" name="exceptions" fileName="(absolute path)\Log\nlog-all-exceptions-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <target xsi:type="File" name="allfile" fileName="(absolute path)\Log\nlog-all-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <logger name="kurs_ASP_dotNET___Restaurant_API.Controllers.*" minlevel="Error" writeTo="exceptions" /> <logger name="kurs_ASP_dotNET___Restaurant_API.Middleware.RequestTimeMiddleware" minlevel="Trace" writeTo="request-time" /> </rules> </nlog> 还有我的示例文件尝试记录错误。 namespace kurs_ASP_dotNET___Restaurant_API.Services; public class RestaurantService : IRestaurantService { private readonly ILogger<RestaurantService> _logger; public RestaurantService(ILogger<RestaurantService> logger) { _logger = logger; } public void Delete(int id) { _logger.LogError($"Restaurant with id: {id} DELETE action invoked"); ... } } 此代码片段应创建“nlog-all-exceptions-${shortdate}.log”文件并记录 .LogError 的每次使用 似乎您可能需要更新特定记录器的规则;尝试一下: <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <logger name="kurs_ASP_dotNET___Restaurant_API.Controllers.*" minlevel="Error" writeTo="exceptions" /> <logger name="kurs_ASP_dotNET___Restaurant_API.Middleware.RequestTimeMiddleware" minlevel="Trace" writeTo="request-time" /> <logger name="kurs_ASP_dotNET___Restaurant_API.Services" minlevel="Error" writeTo="exceptions" /> </rules>

回答 1 投票 0

Nlog配置加载最佳实践

我正在做一个 NLog 记录器包装 dll 以在某些项目中使用,我想提供多个选项来加载配置文件。 NLog 默认路径 参数指定的文件 Nlog 配置文件路径

回答 2 投票 0

模拟NLog的记录器并读取记录的消息

我使用 NLog 4.5.11 进行日志记录,使用 moq 4.10.1 进行模拟。 我有一个中间件,它使用 NLog 将异常详细信息写入日志文件。 我需要在我的 API 项目中对中间件进行单元测试并检查...

回答 2 投票 0

NLog 仅当提供特定名称的事件属性时才写入日志文件

我需要登录使用 NLog 动态创建的多个文件。假设我们想要将每个部门记录到各自的文件中,并将服务消息记录到默认文件中。为了...

回答 1 投票 0

为什么NLog不每天归档

NLog 存档每天都不起作用。但它每分钟都有效 NLog版本5.0.7 这是我的配置 NLog 每天存档不起作用。但它每分钟都有效 NLog版本5.0.7 这是我的配置 <target name="Httpfile" layout="${longdate} ${uppercase:${level}} correlationId-${activityid} - ${message} ${exception:format=tostring}" xsi:type="File" fileName="${logFullName}-http-api-${shortdate}.log" archiveFileName="${logFullNameArchive}-archive-Http-api-${shortdate}-{#####}.zip" archiveEvery="Day" concurrentWrites="true" keepFileOpen="false" archiveNumbering="DateAndSequence" maxArchiveFiles="30" maxArchiveDays="7" enableArchiveFileCompression="true" /> 使用 archiveEvery="Minute" 效果很好 不支持在NLog FileTarget中混合静态和动态归档逻辑。 使用静态归档逻辑时,不应在 ${shortdate} 和 fileName="..." 中使用 archiveFileName="..."。 像enableArchiveFileCompression="true"这样的一些功能仅受静态归档逻辑支持: <target name="Httpfile" layout="${longdate} ${uppercase:${level}} correlationId-${activityid} - ${message} ${exception:format=tostring}" xsi:type="File" fileName="${logFullName}-http-api.log" archiveFileName="${logFullNameArchive}-archive-Http-api-{#####}.zip" archiveNumbering="DateAndSequence" archiveDateFormat="yyyyMMdd" archiveEvery="Day" concurrentWrites="true" keepFileOpen="false" maxArchiveFiles="30" maxArchiveDays="7" enableArchiveFileCompression="true" /> 另请参阅:https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

回答 1 投票 0

压缩前一个日志时,NLog 是否会阻止日志记录?

我们的应用程序使用 NLog 进行日志记录。 日志记录是同步执行的。每天都会创建一个新的日志文件。日志压缩已启用。日志配置以编程方式完成: 日志文件 = 新

回答 1 投票 0

如何在运行时提高NLog日志级别

我的应用程序在程序开头有太多的log.Trace(),导致程序出现故障,所以我无法使用通用的NLog配置来提高日志级别。 所以,我愿意

回答 1 投票 0

NLog 在ConfigureServices 中写入日志会导致日志在整个应用程序中丢失${aspnet-request-url} 和${aspnet-mvc-action}

我使用 NLog 在我的 Web API 应用程序上写入日志(.Net 6) 我需要写入ConfigureServices 方法内的日志。 由于此时ILogger还无法使用,所以我就用这种方式来写...

回答 1 投票 0

NLog 实例化的线程

我们计划在性能关键系统上使用 NLog。其他几个应用程序正在发送 将事件记录到 LogServer 应用程序,该应用程序使用 NLog 将数据写入多个文件目标。 (~10) 什么

回答 1 投票 0

使用自定义目标将所有 NLog 输出重定向到 Serilog

作为从 NLog 切换到 Serilog 的一个步骤,我想重定向 NLog 的 LogManager.GetLogger(name) 标准调用的标准接线,以桥接任何代码日志记录到 NLog 进行转发

回答 1 投票 0

使用 NLog.Targets.ElasticSearch 在 Elasticsearch 中没有日志数据

我正在尝试使用 NLog.Targets.ElasticSearch 通过 NLog 登录 Elasticsearch。 索引创建成功,但未找到日志数据。 我的 NLog 配置: 我正在尝试使用 NLog.Targets.ElasticSearch 通过 NLog 登录 Elasticsearch。 索引创建成功,但是没有找到日志数据。 我的 NLog 配置: <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true"> <extensions> <add assembly="NLog.Web.AspNetCore"/> <add assembly="NLog.Targets.ElasticSearch"/> </extensions> <targets async="true"> <target name="elastic" xsi:type="ElasticSearch" index="logs-elasticsearch-sample" documentType="" uri="http://localhost:9200" includeAllProperties="true" layout ="API:logs-elasticsearch-sample|${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" > </target> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="elastic" /> </rules> </nlog> 我正在使用: .net 6 NLog.Targets.ElasticSearch v7.7.0 NLog.Web.AspNetCore v4.14.0 Elasticsearch v7.17.1(也尝试过 v8.1.0) 原教程: https://dev.to/majidqafouri/writing-logs-into-elastic-with-nlog-elk-and-net-5-0-246c 说实话,我是 elasticsearch 的新手,所以我可能做错了什么,但步骤相当简单。 https://www.elastic.co/guide/en/kibana/8.1/docker.html https://www.elastic.co/guide/en/kibana/7.17/docker.html 我在测试中使用 docker 容器进行 elasticsearch 和 kibana。 还尝试使用控制台应用程序(.net core 3.1)进行日志记录,同时以编程方式为 Nlog 创建弹性目标,我得到了相同的结果。 顺便说一句,我可以从 kibana 创建和查询数据。 对于任何对 NLog.Targets.ElasticSearch 有问题的人来说。 需要考虑的几点: DocumentType 在 v7.x 中已弃用,并在 v8.x 中被删除,因此您必须在 v8.x 中将其设置为空 elasticsearch 索引应该小写 如果您的索引以logs-*开头,请确保为其创建具有更高优先级的索引模板:https://discuss.elastic.co/t/cant-create-indices/254204/3 将此配置添加到 NLog.config 以获取发现问题的日志 <nlog autoReload="true" throwExceptions="true" internalLogLevel="Trace" internalLogFile="c:\work\log.txt"> 如果日志到弹性有任何问题可以在'c:\work\log.txt'上找到

回答 2 投票 0

无法使用 API 密钥与 NLog.Targets.ElasticSearch 进行身份验证

我无法让 NLog.Targets.ElasticSearch 使用 APIKey 连接到本地 ElasticSearch 实例。 我的 nlog.config 有以下内容: 我无法让 NLog.Targets.ElasticSearch 使用 APIKey 连接到本地 ElasticSearch 实例。 我的nlog.config有以下内容: <target xsi:type="BufferingWrapper" name="elasticSearch" flushTimeout ="5000"> <target xsi:type="ElasticSearch" uri="https://dev.myinstance.com:9200" includeAllProperties="true" apiKey="MUhUUG1ZQUJFWE5GNDlPT3J5S3c123456783JRQS1CM1JtQVJzUWppdw==" apiKeyId="1HTPmYABEXNF49OOryKw" documentType="" index="test-${date:format=yyyy.MM.dd}" name="test"> </target> </target> API 密钥已使用curl 验证: curl --insecure -H "Authorization: ApiKey MUhUUG1ZQUJFWE5GNDlPT3J5S3c123456783JRQS1CM1JtQVJzUWppdw==" https://dev.myinstance.com:9200/_security/api_key?pretty { "api_keys" : [ { "id" : "1HTPmYABEXNF49OOryKw", "name" : "test_import", "creation" : 1651847966642, "invalidated" : false, "username" : "elastic", "realm" : "reserved", "metadata" : { } } ] } 然而,nlog 无法验证: Exception: Elasticsearch.Net.ElasticsearchClientException: Failed to ping the specified node. Call: Status code 401 from: HEAD ---> Elasticsearch.Net.PipelineException: Failed to ping the specified node. ---> Elasticsearch.Net.PipelineException: Could not authenticate with the specified node. Try verifying your credentials or check your Shield configuration. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. 我尝试将 requireAuth 添加到 nlog.config,但没有成功。 我也遇到了一些问题。 就我而言,问题与授权标头格式不正确有关。 我正在使用 Serilog.Settings.Configuration 所以我的所有配置都位于 appsettings.json 在 docs 标题中格式为: Authorization: ApiKey ... 但是如果您使用Serilog.Settings.Configuration分隔符号是= Authorization=ApiKey ... 取决于NLog版本 在版本 4 中我可以使用此配置连接并添加日志 我认为你应该使用用户名和密码而不是Apikey includeAllProperties="true" documentType="" requireAuth ="true" username ="xxxxxx" password ="xxxxxxxxxx"

回答 2 投票 0

为什么NLog WriteToNil没有过滤掉指定的记录器?

我按照此处的说明在 .Net MAUI 应用程序中使用 NLog:https://github.com/NLog/NLog.Targets.MauiLog 日志记录设置得很好,没有问题。但是,我想过滤掉一些冗长的内容......

回答 1 投票 0

MAUI 项目中的 NLog.config 文件应该放在哪里?

我们目前开始将 vom xamarin 切换到毛伊岛。在第一步中,我想通过 NLog 实现日志记录。在遇到一些启动问题后,我让它在 iOS 上运行。当我尝试运行它时

回答 1 投票 0

无法加载文件或程序集“AWS.Logger.Core”

我创建了一个新项目并添加了 Nlog.AWS.Logger.i 已使用最新版本的“AWS.Logger.Core”(1.3.1.0)创建了 AWSTarget 对象,当我运行它时,它给出了以下内容错误...

回答 1 投票 0

NLog:如何缩进范围并使用范围标题?

如何在 NLog 中启用以下功能来生成所示的输出: 使用 (_logger.BeginScope("开始处理")) { _logger.LogInformation("算法1已初始化");

回答 1 投票 0

使用 NLog 记录奇怪的字符串时:ArgumentException:“路径中存在非法字符。”

我正在尝试在日志文件中记录以下字符串: $"{(char)0}{(char)1}{(char)3}{(char)0}{(char)3}{(char)0}{(char)0}{(char)0} {行}{(字符)0}{(字符)7}{(字符)232}{(字符)3}{(字符)0}{(字符)9}{(c...

回答 1 投票 0

为什么 NLog 无法将日志存储在 Azure Blob 存储中?

我正在开发一个 ASP.NET Core 6 MVC 应用程序“AzureLogging”,测试使用 NLog 将日志写入 Azure Blob 存储。我遇到了无法在 Azure Blob 存储中存储日志文件的问题,...

回答 1 投票 0

配置 NLog 以写入 ASP.NET Core MVC 应用程序中的网络文件路径

我们正在尝试对使用 NLog 使用文件目标进行日志记录的 ASP.NET Core MVC 应用程序进行负载平衡。负载平衡设置将有多个应用程序服务器,计划是存储

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.