✅ Catching exception at each function or globally?
Team , I am developing .net 6 web api project. Is it good to catch a exception at each function and log or globally handle it. Any suggestions?
35 Replies
Just let them bubble up
Exception handling middleware will handle them
Exception handling middleware means?
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-7.0
A middleware that catches exceptions, logs them if needed, and returns an appropriate response
Oh Ok this is what I meant by globally .... But Can we know from which function I got the exception? or will it just give the info that something has failed ?
The stack trace will tell you
Ok got it
Thank you for clarifying
Otherwise you could use NLog.
Thats a curious way to spell
Serilog
Nuh uh.
You're bullying me.
I don't like it.
not at all, just haven't seen anyone recommend NLog in... 10+ years
structured logging came and took the world by storm, since log files are rarely how we consume logs these days
ELK, Seq and other log aggregation platforms + a structured logging system makes life so much easier
now I'm curious why
Serilog is great
Why would i enjoy getting bullied?
Why don’t you like NLog?
It's less "I don't like NLog" and more "I really like Serilog because it's powerful and flexible and has lots of existing sinks"
I like NLog because it allows me to easily log them in a file.
Did you mean "to a file"?
Yes.
I just woke up.
Serilog also has a file sink
I've never tried Serilog, how would one go about setting up a file sink?
GitHub
GitHub - serilog/serilog-sinks-file: Write Serilog events to files ...
Write Serilog events to files in text and JSON formats, optionally rolling on time or size - GitHub - serilog/serilog-sinks-file: Write Serilog events to files in text and JSON formats, optionally ...
It's like NLog.
Does NLog do structured logging?
Serilog does seem to have more flexibility with their options.
Like?
https://datalust.co/seq this kind of stuff
Seq
Seq — centralized structured logs
Seq is the self-hosted search, analysis, and alerting server built for structured log data.
JSON formatted data?
Matt's Blog of Doom - Home
Structured Logging with Seq and Serilog
A few years back, I picked up an old 'unloved' business application for document handling, and brought it into the modern era. I completed some work on adding automated OCR, running as a service, and then started to enhance it well beyond its original capabilities, such as moving a manual letter creation and printing process into the application...
That's one way to represent structured data
Yes.
NLog has that.
This is just an interface reading from structured data.
Ik, but it was just an easy way to show what you can do with structured logging
I've built my on interface to work with NLog.
I'm not worried about that.
NLog has JSON and Text support.
Like Serilog does.
I might take a look at Serilog.
i did something like this, but would like some experts opinion. i might still be getting unhandled exception in my console. Meaning every time i throw an exception i get unhandled exception message in terminal plus the message of my logger that i made in this middleware
Thank you all for the inputs.. I will explore serilog , can we write logs to sqlserver db using serilog ?
Yes, but relational databases aren't the greatest place to store logs https://github.com/serilog-mssql/serilog-sinks-mssqlserver
GitHub
GitHub - serilog-mssql/serilog-sinks-mssqlserver: A Serilog sink th...
A Serilog sink that writes events to Microsoft SQL Server and Azure SQL - GitHub - serilog-mssql/serilog-sinks-mssqlserver: A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
A dedicated log store like Seq is a better fit
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.