代码之家  ›  专栏  ›  技术社区  ›  Eric

ASP.NET Core 2.1站点的nlog设置

  •  -1
  • Eric  · 技术社区  · 6 年前

    我正在尝试设置nlog以登录到3个单独的日志文件中。我想实体框架的东西去一个日志文件(数据库日志). 我只希望与应用程序相关的日志记录转到另一个日志文件(拥有.log). 最后,我希望其他所有东西(微软的东西)都转到另一个文件(全部.log)

    这是我下面的,但我不能让日志工作,根据我的要求。我做错什么了?

    提前谢谢!

    <?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"
          internalLogFile="internal.log"
          internalLogLevel="info" >
    
      <!-- 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="allfile" 
                fileName="all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
    
        <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web"
                fileName="own-${shortdate}.log"
                layout="${longdate}|${activityid}|${level:uppercase=true}|${message} ${exception:format=ToString}|${logger}|${all-event-properties}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    
        <target xsi:type="File" name="sqllogfile"
                fileName="db-${shortdate}.log"
                layout="${date}|${activityid}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
    
      </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.EntityFrameworkCore.*" minlevel="Trace" writeTo="sqllogfile" final="true" />
        <logger name="Microsoft.*" maxlevel="Info" final="true" />
    
        <!-- Our log -->
        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
      </rules>
    </nlog>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Eric    6 年前

    移动数据库日志是关键。另外,在将catch all日志移到底部之后,我现在得到了正确的日志结果。