C# Logging with Serilog and Seq - Structured Logging Made Easy
Vložit
- čas přidán 19. 06. 2024
- Structured event logging is so much more than just writing a message to a file. Since .NET Core natively supports structured event logging, I wanted to introduce a logger that takes advantage of these additional features. Serilog is an easy to configure and easy to use logging system that you can plug into a .NET Core project within minutes. Today we are going to configure Serilog, see how to use it in a basic logging scenario, then take advantage of the structured logging by hooking up a visual event sink called Seq.
Mailing List: signup.iamtimcorey.com/
Intro to Logging: • Logging in .NET Core 3...
Getting Started with Docker: www.iamtimcorey.com/p/getting...
Source Code: leadmagnets.app/?Resource=Log...
0:00 - Intro
1:29 - Getting started: ASP .NET Core application
4:18 - Built in logging system crash course
8:08 - Exceptions in log messages (built in)
10:27 - Additional information in log message: variables in the log message (built in)
13:42 - Setting up Serilog
17:33 - Serilog NuGet references and configuration
21:38 - Logging application start and crash with Serilog
24:35 - Use Serilog over default built in logging system
25:00 - appsetings.json
29:47 - Serilog Enrichers: settings and NuGet references
32:27 - Writing the log messages to Console
34:29 - Logging request messages
38:11 - Writing the log messages to text file
40:49 - Structured logging: writing the log messages to json file
46:42 - Seq
48:39 - Setting up Seq with docker: NuGet reference, configuration, docker setup
59:15 - Logging with Seq
1:02:34 - Summary and concluding remarks
Tim Corey you are the man, there so much outdated info on serilog on the net
I am glad you got value out of it.
Fantastic tutorial - especially running Seq on Docker. Thank you.
You are welcome.
Watching this again 6 months later and it really helped me figure our the Seq sinks setup for a new project, thanks Tim :)
Great!
Thanks for this. I wanted to add Serilog to my open source Analogy log viewer that supports many formats and now I can learn better how it works
Awesome!
I used to watch TheNewBoston and his tutorials and what have you. Then I found you. I have recommend you to so many people who want to get into c#. Keep up the fantastic videos!
Awesome! I appreciate the recommendations.
Great Video...I really appreciate your videos especially when you introduce new nuget packages that I didn't know before.
Thank you!
Man idk what I'd do without you, I've gotten into C# coding thanks to you! Been watching all of your videos trying to follow your practices. Thank you!
I'm glad my content has been so helpful.
Thanks for your work, Tim!
Your explanation skills make every point that you cover clear and logic, it's just a pleasure to watch your tutorials!
My pleasure!
I was just looking for info on Serilog and found you just uploaded today. Thank you!
Awesome!
Awesome Tim! I have learned a lot about Serilog. Really appreciate your work.
Excellent! I am glad it was helpful.
this was great man, it was the most clear course i saw recently.
i will now sign up to become a patron, keep it up.
Welcome aboard!
Thanks for saving me hours of googling.
With the right amount of Docker lectures.
Please do something on docker and docker compose for us probably with graylog installation.
Thanks
Awesome! I'm glad it was so helpful. As for Docker, I've shown it off a few times on this channel but I also built a course covering how to get started well with it: www.iamtimcorey.com/p/getting-started-with-docker
Hi Tim, your tutorial makes the concept so easy and clear, thanks a lot bro
You are welcome!
Couldn't asked for anything more, great job.
Thanks!
Tim that is an awesome tutorial. I never thought it would be that easy.
Thanks!
Hi Tim, great article as with all of you articles :) Thank you.
You are welcome.
thank you for your great videos. they so practical and filled with details
You are welcome.
Thanks, great tutorial! I ended up using Serilog with InvisionLog instead of Seq. No setup hassle so you can start logging right away
Excellent!
How did you configure InvisionLog in Appsettings? I can't seem to find anything about that :)
Sir, i am new to Serilog. After wathching this video now i know everything what i need for my project. Thank you!
Excellent!
fantastic intro into Serilog. thank you so much.
You are welcome.
Thank you for sharing this. I like your approach to being a teacher. Keep on going.
You are welcome.
Thanks for this awesome tutorial. It helped me a lot with switching my project to Serilog
Great to hear that!
This is so valuable, thank you very much for this video i was able to adapt this and now have a centralized logging server for my Applications. i cant wait to try more things out with this and how Seq comes up comparing with Application Insights (much pricier, especially if you dont know exactly what are you doing).
Thanks for watching and for sharing how you applied the lesson in the real world.
very nice information Tim. Serilog is really a very important tool. Also you said a very important point at the end - "Logging can be very powerful as long a you use it. If you don't use it why bother" -:)
Glad you enjoyed it.
Setting the playback speed to 2x works best. Thanks Tim.
You are welcome.
Woooooowwwww What an amazing explanation!!! Thank you so much.. this is a Loggin master class
You are most welcome. Thanks for watching.
Great videos. I bought your book after discovering your channel. Thank you!
You are welcome. What book are you talking about, though?
Very Nice, Thanks for your work, Tim!
You are welcome.
really appreciate what you do, keep it up!
thank you!
You are most welcome. Thanks for watching.
Hi Tim really appreciate this video. Good job. Thanks.
You are welcome.
Nicely done as always.. thanks tim
You are welcome.
Really helpful tutorials. Thanks a lot for your content
You are welcome.
Awesome video, thanks for your contribution.
You are welcome.
Great tutorial! So informative! Thanks
Glad it was helpful!
Excellent video, thanks Tim
Very welcome
This is awesome. Love it!
Thanks!
Amazing explanation. Loved it.
Glad you liked it!
Please keep doing what you are doing, your learning videos are top notch. U are truly a gem in the c# community.
Thank you!
Thanks Tim, good information!
You are welcome.
Very nice! I learned a lot about Serilog. I appreciated the explanation for background items, like to identify the official Serilog packages vs the random ones.
Glad it was helpful!
Amazing, informative video, thank you.
You are welcome.
Great vid really helps a lot!
Thank you!
Love it, you are a great tutor.
Thank you!
Many Thanks , Very useful video.
You are welcome.
Love this! Thank you!
You are welcome.
Really good tutorial!
It's works!
Glad to hear that!
Tim, you are the best teacher in the world
Thank you!
Excellent tutorial mate.
Glad you liked it
Great tutorial! (as usual)
Only thing I miss is a list of installed packaged in description. I've seen the whole video and now I need to review it to write down needed nuget packages.
If you create a list of them, let me know in a new comment (I don't see threaded comments) and I'll review them and post it in the description for the next person.
Your videos are the best man!!
Thanks for trusting Tim
This looks awesome 😍
Thanks!
very informative, thank you
You are welcome.
Great Work!
Thanks!
Hi, Thank you so much for this amazing video, I just have one question, is it possible to run multiple projects with one Seq to log all actions?
53:53 - when you want to make fonts bigger - HOLD CTRL and scroll mouse wheel (UP/DOWN) to make font bigger/smaller. ;)
Good Tutorial as always Tim!
Thanks for sharing the tip!
Yep, that affects the zoom on the code itself. It doesn't affect the menus or other windows, though.
@@IAmTimCorey I don't understand though how that matters, and since you mention it, it must mean something..
I thought the point was to make what you type bigger so it can be easier to read, no?
What menu do I need to be bigger in PowerShell ? Thanks for great tutorials as always!!
As usual a great tutorial from my favorite teacher
thank you a lot for your time and effort
but you said in the "Intro to Logging" tutorial, logging is expensive you need to do it in the background
any suggestions on how to do that
does use async/await is efficient to do so ???
also is there any free tool for logging has the same functionality as seq
Very Helpful!
Great!
Lovely. I would like to know if I can have different logging files. For instance one for errors and the other for any other information. This means I would want to have a separate file for errors
Thaks for this awesome video. Running into an issue where I would like to turn off a particular context that just throws out grabage error level messages. Any idea how to do this? For now i set the override to fatal.
Thank you it helps👍👍👍👍
You are welcome.
Hey Tim. I am loving your videos. Keep up the good work. Just one comment though, can you zoom into the code editor when making these great tutorials so I can see the code better? I'm watching this on my phone and can *just* about make out what you are typing. Many thanks
I do what I can but with so much code to show, it is not usually practical to zoom in to that level.
Serilog is one of the things every person new to programming should learn out of the gate, especially structured logging, because it makes ones coding life soooo freaking much easier. And you are right about their documentation -- it is superb.
I agree.
Thank you so much
You're most welcome
Thanks for the video. I need help in rolling out the log file . once it reaches the size of say 2 MB then create a new log file. How to achieve that please help.
Dude, I love you.
Thanks!
@@IAmTimCorey Maybe you could make an 'Education' type page on LinkedIn? I record my certs there, Udemy, Pluralsight etc ... but to reference youtube doesn't carry much weight. So if you set up a page of type education I could reference it to you (and then so could others!). Example: www.linkedin.com/in/robertmulpeter/
I just launched a LinkedIn learning page. I'll make it prettier in the coming weeks but here you go: www.linkedin.com/company/iamtimcorey
This is a great video!!! I'm also concerned about how to maintain the logs so that they don't take a huge amount of space and then I run into trouble with my IT
Only log what you are going to use.
Tim, how can we create logs per each call and how can I define custom path. Ex. file name with {production}/{date}/{guid}.txt
I used to use NLog to write errors to text files or database table. After watching this I just upgraded the new application that Im working to Seq with Serilog. I'll be checking out other sinks and stuff because it's so easy the way you explained it just to set up sinks in the appsettings.json file. That's awesome.
Great!
Great tutorial. I was wondering where I could get information on how to use Serilog with a .NET Core console app, you said in the video that you have to do things differently than for a web app?
I demonstrate that in my Foundation in C# module 7 course, but I don't think I did it on this channel yet. They have made it easier with HostBuilder now. Maybe I'll add that to my suggestion list.
That's a great tutorial you have there. It's just a shame I don't think I could get my organisation to grab a paid subscription to Seq.
In the meantime, I'll plod on with the Serilog mssqlserver sink, and see if I can figure out how to filter out sensitive data with it.
Maybe show them the benefits of Seq and how it will improve efficiency. Sometimes, showing how much an item will save you compared to how much it costs can be beneficial (I know it won't always).
How did you know I was going to need this three days later? Just learned about Serilog/Seq today and BOOM! Tim Corey video.
EDIT: Question. If I set my Default minimum level to "Warning", can I put an override for my own namespace to be Information? It's not working so I'm curious if that's possible at all.
I don't believe it works that way. The global is the minimum level so you would need to go the opposite way and set everything to information and then set all namespaces but yours to warning.
Tim, do you recommend using the built in .Net core logging in your code and then hooking up serilog integration with it at the end?
I definitely recommend you put logging in your application. What you choose to log to can change over time. You can start with just the console and move to Seq for production. That would be a good option.
Loved the vid, good info! I did have a question thou, how about having multiple clients logging to Seq?
But after some research I connected the dots:
- Docker: you can mount multiple volumes (using the v: multiple times)
- Seq: creating API keys in the Settings-page (with Applied Property: ApplicationName) and consuming it in the clients
- Seq: in the overview you can use Signals > Application = 'ApplicationName' to filter on each client
Not that advanced but I guess not everyone will find it useful. But seeing web services are becoming very common now, it might be nice to know.
Thank you for sharing what you found.
Thanks, 😁Could you also do video about serilog with elastic search,kibana and your opinion etc
I will add it to the list. Thanks for the suggestion.
Incredible content Tim! Thank you.
Wanted to understand if you ever mixed file logging with Seq.
Let me explain, ELK typically is used with file beat so that it could tail docker logs and add them to Elastic.
I am wondering if you ever implemented that with Seq.
I wanted to keep file logging but having some kind of file beat reading them and putting them in Seq.
Am interested in this as well, how'd yours go
Thank you for the great video! I have a question: how to log to separate sinks different messages using different templates. For instance: console, two files, and logentries. Thank you
You can define what gets written to each sink in the configuration file. You would need to look up the way to configure each sink.
I'm trying to find info on how to implement this with .net core 3 wpf app. documentation is lacking, and I'm not sure where to start as my wpf application doesn't have a startup.cs...
thank you
You are welcome.
Hi Tim,
Thanks for this wonderful tutorial. I have a question for you.
How can we turn off logging in appsettings either for .net core logging or if using Serilog, how to turn off a particular sink in appsettings file ?
Usually you do this through the appsettings.json file by setting the threshold to off or to a high value.
Great video. The seq really looks like a powerful tool so thank you for introducing that here. As you you are here working with application json config I am curious how to work with the application.development json. Do you cover that somewhere in your videos?
When dealing with the config automatically set up for you in ASP.NET Core, I don't believe you need to do anything to get that to work. If you are setting up where it pulls from manually, you just need to add that to the configuration setup in the startup.cs file. It is a pretty slick system. You can add in Azure Key Vault as well.
@@IAmTimCorey So would you do something like this:
string jsonConfig;
#if (DEBUG)
jsonConfig = "appsettings.Development.json";
#else
jsonConfig = "appsettings.json";
#endif
?
No, you add both to the config options and you don't worry about the #if statement. The only time you would need that #if statement is if you were manually adding the files instead of configuring it automatically. The system looks for the json file associated with where the item is (in a development environment or a production environment) based upon a flag that is set. If it is not set, it is in release mode. Visual Studio sets it to development. Either way, you wouldn't add the dev version instead of the main version, you would add both because the dev version should just override settings that need overriding. Think of the config options like CSS in that settings in one file can override the other based upon order.
Can i use serilog in production? Its paid one or free open source ?
Hi. Great video!
Just wondering do you have a sample that starts from a Console Application?
Nope, but most of the code will be the same.
Awesome man Awesome , You Are just awesome.. It is not like you cant get code and explanation of serilog on internet , but the way you have explained like - how well structured manner serilog can be implemented and whats the way you should follow to implement serilog in the enterprise level application , thax man. BTW I have sent you mail for source code. I hope I'll get the reply soon. :-)
I'm glad you are enjoying it. I did get your email but haven't responded yet. I'm working through a backlog of email (I've been putting off responding while I record a course).
This is a fantastic video on Serilog and logging in general. Thanks. My question would be performance impact of Serilog. Is there anything significant? I'm hoping it's asyncronous. Fire log event and continue? And then when applying Seq - is it OK to host the SEQ server on a different machine? Would this cause a performance issue - as it posts to a URL. I host a site in a shared hosting environment where I may not be able to install Seq. Can I host that on different infrastrcuture and just pass the URL in appsettings.json?
As always, verify your assumptions but Serilog is very efficient and should not cause a noticeable performance hit on your application unless you are logging expensive things rapidly. As for hosting Seq, you can host it wherever you want. Network latency will be a factor but usually not much of one.
NLOG rules :-) Greate video!!!
Thanks!
Hey Tim. Thanks for this great video. I have 2 question.
1. Does logging so much data will have any impact on the application performance.
2. Can we configure logging like turn logging on and off whenever we need it or activate/deactivate certain logs e.g. Error logging should always be ON and other logs like Information or Warnings can be activated or deactivated whenever there is a need. I mean can this be handled in the appsettings.json itself or do we need to write a logic.
It depends on what you mean by "so much information". You get to choose how much to log. This leads into your second question - you get to choose at runtime what log level to capture. For instance, normally you would capture just warning, error, and critical. However, if an app was having an issue, you could drop the level to information, so you capture more messages and get a better picture of your app. If an entry isn't being recorded, it has practically no impact on your system. My recommendation is to only log what you are going to use. You put all of the log statements into your code to log everything you might need, but then only capture what you will actually read.
At 39:25 can we set a path that's agnostic of weather it is windows or Linux?
This is the fantastic tutorial. I learnt how to use serilog using this video. Apart from that, I wanted to write the log to console in development but to the text file in the production using the same appsettings. Currently, it writes files in both location with appsettings configuration. How can I control this? Do you have any example of the implementation?
You could put one configuration in appsettings.developer.json and the other in appsettings.production.json. Then it should do what you want.
Hello. Is there a way to send logging information from SQL server to Seq?
Hi Tim, I've been following along with the TimCo Retail Manager series and staring to build my own WPF app using the principles you''re using (Caliburn Micro). I'm trying to get my head around how to use SeriLog and register it for use in my ViewModels. Would the SimpleContainer be the way to go or do I need to look at AutoFac?
SimpleContainer should work just fine.
@@IAmTimCorey I'll keep trying with SimpleContainer and see how I get on. Thanks
Hi Tim! Excellent tutorial but he most I like is the teaching style :-)
I have big question though: my project is a webAPI using Core 5. Working fine with no errors before watching your tutorial. Applied your settings and run again with no problems or errors I can see. However, no log file (text or json) is generated or console messages to be seen. It must be an obvious reason but I cannot see it. Any idea?
Thanks again.
Check your appsettings.development.json file to see if you are filtering out more data there. That's a common issue.
Another amazing video, you have made my learning so much easier!
Great!
hey tim, i'm working on a project and want to use serilog by writing it to files with an accurate time stamp for each action have any tips?
Serilog can write a timestamp for each log statement without an issue.
Are there (maybe less fancy) Open Source alternatives to Seq that also visualize structured logs? We really hesitate to bind us to an 8000 USD yearly contract.
Awesome!
I would like to add additional columns, such as 'Test1' and 'Test2,' to a SQL Server database. I also aim to insert data into these columns using a Logger. Could you please suggest a way for me to achieve this?
Whaaat, why
can you tell me how we can enable the blue line that it select he first text in the small box .. i mean that when we write something then that small box appear with method variable class etc
Can you give me a time code in the video where you saw this? Sorry, I'm not quite sure what you are asking for.
Excellent Tutorial.. Thanks a ton.. is it possible to see the xml version of the settings shown in this video for .Net Framework wpf app?
I don't have an example of that, no.
Thank for the video. Maybe you could help on a problem im having.
I have many uses in ILoggerFactory which im using in startup to inject logger to many projects which my main project is using. When removing microsoft logging and only using serilog it doesn`t know ILoggerFactory andi cant compile the project
You should be able to add Serilog to a ILoggerFactory: csharp.hotexamples.com/examples/-/ILoggerFactory/AddSerilog/php-iloggerfactory-addserilog-method-examples.html
Hi Tim..I would like to know which is better whether SeriLog or NLog to log asp.net core web application info and error to the SQL server database
I wouldn't say there is a "better". Choose the one that has the features and design you like better. It is a preference thing.
Hi Tim - awesome course. I know there are ways to get Serilog to create multiple sinks and filter what goes into each - but can not find out if there is a way to get Seq to handle that and repoint to different input sinks. We need this for performance and separation of multi customer views. Do you know of anything?