2017-10-25 16 views
0

.NET 콘솔 프로그램에 NLog를 구성하여 Windows 이벤트 로그에 오류와 치명적인 오류, 파일에 대한 모든 오류 및 정보, 경고 만 기록하도록 시도하고 있습니다. , 오류 및 치명적인 오류를 콘솔에 표시합니다.NLog Configuration :: 어떤 레벨로 어떤 출력으로 이동 제어

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> 
    </startup> 
    <nlog autoReload="true" internalLogFile="log/internal.txt" internalLogLevel="Trace"> 
    <variable name="brief" value="${longdate} ${level} ${logger} ${message}${onexception:inner= ${exception:format=toString,Data}} "/> 
    <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}${onexception:inner= ${exception:format=toString,Data}}"/> 
    <targets> 
     <target name="console" type="Console" layout="${brief}"/> 
     <target name="all_in" type="File" layout="${verbose}" fileName="${basedir}/log/log.txt" archiveFileName="${basedir}/log/archive/log.{#}.txt" archiveNumbering="DateAndSequence" archiveAboveSize="1048576" archiveDateFormat="yyyyMMdd" keepFileOpen="false" encoding="iso-8859-2"/> 
     <target name="events" type="EventLog" layout="${verbose}" source="nlog-test" onOverflow="Split"/> 
    </targets> 
    <rules> 
     <logger name="*" minlevel="Info" writeTo="console"/> 
     <logger name="*" minlevel="Trace" writeTo="all_in"/> 
     <logger name="*" minlevel="Fatal" maxlevel="Error" writeTo="events"/> 
    </rules> 
    </nlog> 
</configuration> 

나는 이것을 테스트하기 위해 간단한 콘솔 프로그램을 작성했다.이 프로그램은 사용자의 명령을 읽고 적절한 수준으로 씁니다. 보시다시피 NLog가 수행하는 작업과 그 이유를 이해할 수 있도록 내부 로깅을 사용하도록 설정했습니다.

이벤트 로그에 오류 (양호) 및 정보 (불량) 쓰기가 시작되었습니다. 이제 이벤트 로그에 아무 것도 쓰지 않습니다. "maxlevel"이 Error로 정의되고 "Info"가 "higher"일 때 "Info"레벨을 이벤트 로그에 쓰는 이유를 이해할 수 없습니다. 이 이벤트 로그에 아무 것도 작성 중지 이유는 다음 maxlevel="Error"를 제거하고 minlevel="Error"minlevel="Fatal"을 변경할 수 있습니다 .. events -logging 규칙에 대한

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Linq; 
using NLog; 

namespace nlog_test 
{ 
    class Program 
    { 
     static Logger logger = LogManager.GetCurrentClassLogger(); 

     static void Main(string[] args) 
     { 
      while(true) 
      { 
       try 
       { 
        Console.Write("nlog-test> "); 

        var operands = Console.ReadLine().Split(
          new[] {' ', '\t'}, 
          StringSplitOptions.RemoveEmptyEntries); 

        if(operands.Length == 0) 
        { 
         continue; 
        } 

        var command = operands[0]; 
        var arguments = operands.Skip(1); 
        Action<Action<string>> log = 
          f => f(string.Join(" ", arguments)); 

        switch(command) 
        { 
         case "debug": 
          log(logger.Debug); 
          break; 
         case "delete": 
          File.Delete("log/log.txt"); 
          break; 
         case "error": 
          log(logger.Error); 
          break; 
         case "fatal": 
          log(logger.Fatal); 
          break; 
         case "help": 
          Console.Write(@" 
COMMAND [ARG...] 

Commands: 

    debug TEXT... 
     Delete the log file. 

    delete 
     Delete the log file. 

    error TEXT... 
     Write an error message. 

    fatal TEXT... 
     Write a fatal error message. 

    help 
     Print this message. 

    info TEXT... 
     Write an information message. 

    print 
     Print the contents of the log file. 

    quit 
     Exit the process. 

    trace TEXT... 
     Write a trace message. 

    warn TEXT... 
     Write a warning message. 
"); 
          break; 
         case "info": 
          log(logger.Info); 
          break; 
         case "print": 
          { 
           var psi = new ProcessStartInfo("less.exe", "log/log.txt"); 

           //psi.CreateNoWindow = true; 
           psi.UseShellExecute = false; 

           using(var process = Process.Start(psi)) 
           { 
            process.WaitForExit(); 
           } 
          } 
          break; 
         case "quit": 
          return; 
         case "trace": 
          log(logger.Trace); 
          break; 
         case "warn": 
          log(logger.Warn); 
          break; 
         default: 
          Console.Error.WriteLine(
            "Unknown command: {0}", 
            command); 
          break; 
        } 
       } 
       catch(Exception ex) 
       { 
        Console.Error.Write($"Caught unhandled exception: "); 
        Console.Error.WriteLine(ex); 
       } 
      } 
     } 
    } 
} 
+0

events-logging-rule의 경우 maxlevel = "Error"를 제거하고 minlevel = "Fatal"을 minlevel = "Error"로 변경하십시오. –

+0

@RolfKristensen 감사합니다. 그게 문제라고 생각합니다. 원래 나는 minlevel을 가지고 있었지만, 어떤 이유로 그것이 오작동이라고 생각하고 그것이 거꾸로 생각했습니다 ... 결국, 그것은 맞았습니다. 또한 왜 내 테스트 프로그램이 이벤트 뷰어에 로깅을 중지했는지 설명합니다. 원래는 max = Error 였고 min = Fatal을 추가했습니다. min = 치명적이고 max = Error는 일치가 없으므로 아무 것도 로그되지 않습니다. 내 계정은 귀하의 코멘트를 upvote만큼 큰 아니지만, 당신이 대답을 게시하려면 그것을 받아 들일 수 있습니다. –

답변

0

을 이해하지 않습니다.