dotnet core 2 project에 nlog추가

nuget 설치

NLog.Web.AspNetCore

nlog설정 추가

nlog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <targets>
    <target name="logfile" xsi:type="File" fileName="logs/nlog-${shortdate}.log" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

Program.cs 변경

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseNLog() //추가
        .Build();

build 설정 변경

nlog.config » property » copy to output directory » Copy always

logs폴더를 배포시 만들어준다.

project.csproj 파일을 수정한다.

<Target Name="CreateLogsFolderDuringCliPublish" AfterTargets="AfterPublish">
  <MakeDir Directories="$(PublishDir)logs" Condition="!Exists('$(PublishDir)logs')" />
</Target>

<Target Name="CreateLogsFolderDuringVSPublish" AfterTargets="FileSystemPublish">
  <MakeDir Directories="$(PublishUrl)logs" Condition="!Exists('$(PublishUrl)logs')" />
</Target>

완료

실행해보면 루트 폴더에 로그가 찍힌다.

teamsmiley's profile image

teamsmiley

2018-04-23 00:00

Read more posts by this author