如何通过Nlog添加跟踪日志使用Castle Windsor LoggingFacility?

时间:2018-10-19 03:25:32

标签: c# asp.net-core castle-windsor nlog

在以前的Netcore项目中我没有使用过城堡,而我的Nlog日志就是这样。

  

2018-10-19   10:46:47.8125 | 2 | Microsoft.AspNetCore.Hosting.Internal.WebHost | INFO |请求   完成于27.8246ms 500 application / json; charset = utf-8 2018-10-19   10:46:47.9736 | 1 | Microsoft.AspNetCore.Hosting.Internal.WebHost | INFO |请求   开始HTTP / 1.1 GET http://yunmatest.fanjiaxing.com/favicon.ico
  2018-10-19   10:46:47.9736 | 2 | Microsoft.AspNetCore.Hosting.Internal.WebHost | INFO |请求   于0.6539ms结束404 2018-10-19   10:46:52.9345 | 1 | Microsoft.AspNetCore.Hosting.Internal.WebHost | INFO |请求   开始HTTP / 1.1 GET   http://yunmatest.fanjiaxing.com/api/Authoriz?businessType=train&unionid=1810150751467754&schoolCode=14541

它将跟踪我的所有请求。但是在城堡里,我不知道如何获得这样的日志。


这是我在城堡项目中的代码。

Startup.cs

IocContainer.Instance.AddFacility<LoggingFacility>(
    p => p.LogUsing<NLogFactory>().WithConfig(@"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="Warn"
      internalLogToTrace="true"
      internalLogFile="internal-nlog.txt">

  <extensions>
    <!--enable NLog.Web for ASP.NET Core-->
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- define various log targets -->
  <!--定义日志文件目录-->
  <variable name="logDirectory" value="${basedir}/logs/${shortdate}"/>
  <variable name="nodeName" value="node1"/>

  <targets async="true">
    <!-- 全部日志target -->
    <target xsi:type="File"
            name="allfile"
            fileName="${logDirectory}/nlog-all/${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${logger}|${uppercase:${level}}|${message} ${exception:format=tostring}"
            keepFileOpen="false"
            />

    <!-- 本地文件日志target -->
    <target xsi:type="File"
            name="ownLog-file"
            fileName="${logDirectory}/nlog-${level}/${shortdate}.log"
           layout="${longdate}|${event-properties:item=EventId_Id}|${logger}|${uppercase:${level}}|  ${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"
            keepFileOpen="false"
            />

    <!-- Tcp日志target -->
    <target xsi:type="Network"
            name="ownLog-tcp"
            keepConnection="false"
            OnConnectionOverflow="Block" 
            MaxConnections="50"
            address ="tcp4://localhost:8001"
            layout="#${longdate}#${nodeName}#${logger}#${uppercase:${level}}#${callsite}#${callsite-linenumber}#${aspnet-request-url}#${aspnet-request-method}#${aspnet-mvc-controller}#${aspnet-mvc-action}#${message}#${exception:format=ToString}#"
            />
    <!--grok 规则-->
    <!--%#{DATA:request_time}#%{DATA:node_name}#%{DATA:class_name}#%{DATA:log_level}#%{DATA:call_site}#%{DATA:line_number}#%{DATA:request_url}#%{DATA:request_method}#%{DATA:container_name}#%{DATA:action_name}#%{DATA:log_info}#%{DATA:exception_msg}#-->
    <!--空白-->
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <!--日志级别 Trace -》Debug-》 Info -》Warn-》 Error-》 Fatal-->
  <!--日志规则-->
  <rules>
    <!--全部日志, 包括Microsoft日志-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--自定义日志,排除Microsoft日志-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Debug" writeTo="ownLog-file" />
    <logger name="*" minlevel="Info" writeTo="ownLog-tcp" />
  </rules>
</nlog>

如何通过Nlog添加跟踪日志使用Castle Windsor LoggingFacility?

0 个答案:

没有答案