首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP.NET内核中用NLog记录异常

在ASP.NET内核中用NLog记录异常
EN

Stack Overflow用户
提问于 2017-06-20 07:10:38
回答 2查看 2.7K关注 0票数 3

目前,我正在尝试使用NLog记录异常消息等,但到目前为止还没有结果,我无法想象为什么

要安装NLog,我使用了以下指南:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)

我已经在asp.net核心上设置了这个,并尝试了在我的业务项目上进行日志记录。

Bussiness

代码语言:javascript
复制
using Microsoft.Extensions.Logging;

public class FileService
{
    private readonly ILogger<FileService> _logger;

    public Task<DataResult<ICollection<BlankDTO>>> Test(Guid appId, Guid fileId)
    {
        try
        {
            throw new DivideByZeroException();
        }
        catch (DivideByZeroException ex)
        {
            _logger.LogError("EventId", ex, "Welp, something went wrong", new object[0]);
        }
    }
}

NLog.config

代码语言:javascript
复制
<?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="Warn"
      internalLogFile="c:\temp\internal-nlog.txt">

  <!-- Load the ASP.NET Core plugin -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
     <!-- write logs to file -->
     <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
                 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />

   <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log">
      <layout xsi:type="JsonLayout">
        <attribute name="level" layout="${level}" />
        <attribute name="timestamp" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}" />
        <attribute name="systemUser" layout="${windows-identity:userName=true:domain=true}" />
        <attribute name="currentUser" layout="${aspnet-user-identity}" />
        <attribute name="machineName" layout="${machinename}" />
        <attribute name="environment" layout="#{Environment}" />
        <attribute name="applicationName" layout="My app" />
        <attribute name="module" layout="My module" />
        <attribute name="threadId" layout="${threadid}" />
        <attribute name="message" layout="${event-properties:item=message}" />
        <attribute name="messageDetails" layout="${exception}" />
        <attribute name="variables" layout="${event-properties:item=Variables}" />
      </layout>
    </target>
     <!-- write to the void aka just remove -->
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

,这是我所得到的全部日志:

代码语言:javascript
复制
{ "level": "Error", "timestamp": "2017-06-20 09:51:24.904", "systemUser": "JONDOE-ORG\\foobar", "machineName": "BUC-007", "environment": "#{Environment}", "applicationName": "My app", "module": "My module", "threadId": "4" }
  • NLog.Web.ASpNetCore (4.4.1)
  • Microsoft.NETCore.App (1.1.2)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-20 11:48:13

我创建了一个LoggingEvents

代码语言:javascript
复制
public class LoggingEvents
{
    public const int GENERATE_ITEMS = 1;
    public const int LIST_ITEMS = 2;
    public const int GET_ITEM = 3;
    public const int INSERT_ITEM = 4;
    public const int UPDATE_ITEM = 5;
    public const int DELETE_ITEM = 6;

    public const int GET_ITEM_NOTFOUND = 7;
    public const int UPDATE_ITEM_NOTFOUND = 8;
}

我可以记录这样的错误:

代码语言:javascript
复制
//Logging example
try
{
    throw new Exception();
}
catch (Exception ex)
{
    _logger.LogError(LoggingEvents.GET_ITEM, ex, "Test({appId}, {fileId} Failed", appId, fileId);
}

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2020-07-06 04:58:02

我不认为你需要这些:

Logger.LogError($“{ex.Message}{{@0}}”,ex,arg0);

logger.LogError(ex.Message,ex,arg0);

和:

Layout=“${date:format=yyyy HH:mm:ss.fff zzz} ${level:uppercase=true:truncate=3} ${level:uppercase=true:truncate=3} ${mdlc:CorrelationId} HH:mm:ss.fff Data}”/>

参见示例和使用库:

https://github.com/akovac35/Logging

https://github.com/akovac35/Logging.Samples

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

https://stackoverflow.com/questions/44646167

复制
相关文章

相似问题

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