Author Topic: Logging with Serilog + Seq  (Read 852 times)

0 Members and 1 Guest are viewing this topic.

CADbloke

  • Bull Frog
  • Posts: 275
  • Crash Test Dummy
Logging with Serilog + Seq
« on: April 23, 2015, 06:31:02 pm »
Serilog is a .NET logging framework that writes log events in a structured, queryable manner. Seq is a http receiver for Serilog. There are plenty of other built-in receivers, called "Sinks", for Serilog so you can write logs to a file, console, database...in any combination. Seq is just one of the receivers, there are plenty of others you can plug into it.

The beauty of Seq is that it can give you a real-time feed of your log events in a localhost page. Have a look at the screenshots at http://getseq.net/ and also http://nblumhardt.com/ <== he wrote it ...read some words too if you have time.

The beauty of Serilog is the structured event data. The screenshots in the links above will show you what I mean by that. My "wow" moment came when I grokked the whole structured, queryable aspect of it.

Seq is free for a single user, you only need  paid license if you deploy to multi-user multi-tier environments where you need API keys to manage traffic.

Serilog is on Github and is free.

I found it very easy to install and get up and running, just install Seq and add the Serilog Nuget package to your Visual Studio solution and you're right to go.

Here's a snippet of code that will make little sense out of context but it's an illustration of structured logging....

Code - C#: [Select]
  1. if (cableNumberAttribute == null)
  2. {
  3.  if (!OffSheetBlocksWithNoCableAttribute.Contains(block.Name))
  4.  {
  5.    Log.Warning("{DwgFileName} - block : {BlockName} has no attribute for a cable number. {OffSheets} {@Tags}",
  6.                Path.GetFileName(db.Filename),
  7.                block.Name,
  8.                OffSheets,
  9.                attributeRefs.Select(a => a.Tag).ToList());
  10.  
  11.    OffSheetBlocksWithNoCableAttribute.Add(block.Name);
  12.  }
  13. }

Notice the {@Tags} property, it will serialize that list into the log so your receiver(s) can deserialize it and display it in a manner native to the receiver.

TLDR; (yes, irony) If you want logging, this is the most awesome logging thing I have seen.

Keith Brown

  • Swamp Rat
  • Posts: 581
Re: Logging with Serilog + Seq
« Reply #1 on: July 14, 2015, 03:08:27 pm »
Hi CADBloke,


I just wanted to mentioned that based on your post I went ahead and implimented logging into my apps utilizing Serilog.  It does work incredibly well and depending on if you want to take the time to implement it correctly can give you a great understanding of what your program is actually doing without having to step through code.  Currently I am only logging to a file but plan on using Seq for all in house logging and will continue to log to a file(s) for any distributed apps.


Anyway I just wanted to thank you for bringing Serilog to the communities attention.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

CADbloke

  • Bull Frog
  • Posts: 275
  • Crash Test Dummy
Re: Logging with Serilog + Seq
« Reply #2 on: July 16, 2015, 05:50:39 pm »
Hi Keith

Glad to hear it works for you too. I've met the author - he made autofac as well. He is plenty clever and nice bloke too. I tend to use the Seq http sink (receiver) and watch the logging in real time in the browser but I can definitely see the value in logging to a file. You can, of course, do both just by changing the constructor call.

Glad to hear it was useful.
:)

CADbloke

  • Bull Frog
  • Posts: 275
  • Crash Test Dummy
Re: Logging with Serilog + Seq
« Reply #3 on: February 27, 2017, 09:34:32 pm »
Greetings from the past / future. An update to this, I still use Serilog everywhere, these days I watch the logs in real time using https://github.com/RolandPheasant/TailBlazer (which is also a great study of WPF and Reactive Extensions).

Keith Brown

  • Swamp Rat
  • Posts: 581
Re: Logging with Serilog + Seq
« Reply #4 on: March 02, 2017, 10:31:41 am »
Greetings from the past / future. An update to this, I still use Serilog everywhere, these days I watch the logs in real time using https://github.com/RolandPheasant/TailBlazer (which is also a great study of WPF and Reactive Extensions).


How is watching it in real time different than doing so with Seq?  I use Serilog and Seq in all of my coding and have found it to be a great help in debugging.  I am just wondering how using TailBlazer would enhance that.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

CADbloke

  • Bull Frog
  • Posts: 275
  • Crash Test Dummy
Re: Logging with Serilog + Seq
« Reply #5 on: March 02, 2017, 09:08:56 pm »
How is watching it in real time different than doing so with Seq? 
In real time it's pretty-much the same thing with a few differences like filtering, tag handling etc. Tailblazer can open any log so you can review older logs in the same app. You also don't need to install and run Seq. You can also tail logs in Notepad++ these days but I haven't found that to be a smooth experience.

It has been a couple of months since I used either (damn that day job!) but yeah, they're both awesome.