DevSight

C#에서 AOT로 NLog사용하기

이전 글 C# AOT 라이브러리 활용에 이어 이번 글에서는 로그 시스템으로 많이 사용하는 NLog를 AOT (Ahead-of-Time)로 빌드하여 라이브러리로 사용하는 법을 알아본다.

실행파일과 같은 폴더에 AppLogs 폴더를 만들고 여기에 연월/날짜/로그타입별파일로 기록하고 디버그 모드로 빌드 후 실행 했을때 DebugViewPP로 실시간 로그를 볼 수 있도록 하였다.

기본 패키지의 리플랙션(Reflection)1을 피해서 작성해야 하므로 기본 사용법과 차이가 있을 수 있다. 참고로 Avalonia + MVVM로 구성된 프로젝트를 AOT로 빌드할 수 있도록 주요 패키지를 하나씩 옮겨보려고 한다.

LogHelper(AOT)
  • 프로젝트(LogHelper) 구성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<Project Sdk="Microsoft.NET.Sdk">  
    <PropertyGroup>  
        <TargetFramework>net10.0</TargetFramework>  
        <ImplicitUsings>disable</ImplicitUsings>  
        <Nullable>enable</Nullable>  
        <PublishAot>true</PublishAot>  
        <IsAotCompatible>true</IsAotCompatible>  
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>  
        <OptimizationPreference>Speed</OptimizationPreference>  
    </PropertyGroup>  
  
    <ItemGroup>  
        <PackageReference Include="NLog" Version="6.1.1"/>  
        <PackageReference Include="NLog.OutputDebugString" Version="6.1.1"/>  
    </ItemGroup>  
  
    <Target Name="CopyLogHelperDebug" AfterTargets="Publish">  
        <Copy Condition="'$(Configuration)' == 'Debug'"  
              SourceFiles="$(PublishDir)LogHelper.dll"  
              DestinationFiles="$(PublishDir)LogHelper.Debug.dll"  
              SkipUnchangedFiles="true"/>  
        <Copy Condition="'$(Configuration)' == 'Release'"  
              SourceFiles="$(PublishDir)LogHelper.dll"  
              DestinationFiles="$(PublishDir)LogHelper.Release.dll"  
              SkipUnchangedFiles="true"/>  
    </Target>  
</Project>
  • 소스 AOT(LogHelper.cs)
Read More ···