How to capture console output from a service C#?

后端 未结 7 1579
无人及你
无人及你 2021-02-07 06:12

We have a C# service that is deployed to a remote customer system. The application writes a substantial amount of \"diagnostic\" information to the console (i.e. Console.WriteL

7条回答
  •  悲&欢浪女
    2021-02-07 06:30

    You've got a bunch of options; redirecting console output to a file and using a proper logging library as mentioned are two good ones. Here's a middle option: write to the event log.

    EventLog log;
    string logsource = "MyService";
    
    // execute once per invocation
    if (!System.Diagnostics.EventLog.SourceExists(logsource))
    {
        System.Diagnostics.EventLog.CreateEventSource(
            logsource, "Application");
    }
    log = new EventLog();
    log.Source = logsource;
    log.Log = "Application";
    
    // replace console logging with this
    log.WriteEntry(message, EventLogEntryType.Information);
    

    Then look for entries in the Application event log (Administrative Tools -> Event Viewer) where Source = "MyService".

提交回复
热议问题