Rauf
Rauf

Reputation: 12842

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.

Upvotes: 1

Views: 3310

Answers (2)

Justin
Justin

Reputation: 86729

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, e.g.

{"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 (e.g. 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.

Upvotes: 2

Matteo Sangalli
Matteo Sangalli

Reputation: 29

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)

Upvotes: 1

Related Questions