简体   繁体   中英

Logging in C# Windows Service

What is the best way to 'log' Windows Service activity, should I use Windows Log Event Viewer, or just use plain txt file. If I use event log, I think there is a chance of being the event log full.

My new favourite logging framework is Serilog . We make use of ELK for log aggregation and so Serilog is setup to write logs JSON format, eg

{"Level":"Information","MessageTemplate":"Service Started","Timestamp":"2017-07-27T11:29:54.3948669+01:00"}
{"Level":"Information","MessageTemplate":"Service Stopped","Timestamp":"2017-07-27T11:31:14.8305763+01:00"}

These logs are then sent for aggregation and are visible and searchable via a web interface - we don't bother logging to the event log because nobody logs onto these boxes.

If you only have a couple of services then log aggregation might be overkill - its easy enough to have Serilog write to other formats instead (eg to the event log, or non-json formatted files which are easier for humans to read).

Definitely use a logging framework ( log4net is also good, I've also heard good things about NLog but never used it myself). Any good logging framework will let you selectively log important messages to the event log.

I would use the NLog library. link Then you are free to choose File or EventViewer depending on the customer expectations.

From the support people point of view I would use the event viewer because it is easy to reach. (but do not flood it with info messages, just error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM