C
C#15mo ago
Shiv

✅ 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
Angius
Angius15mo ago
Just let them bubble up Exception handling middleware will handle them
Shiv
ShivOP15mo ago
Exception handling middleware means?
Angius
Angius15mo ago
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
Shiv
ShivOP15mo ago
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 ?
Angius
Angius15mo ago
The stack trace will tell you
Shiv
ShivOP15mo ago
Ok got it Thank you for clarifying
Azrael
Azrael15mo ago
Otherwise you could use NLog.
Pobiega
Pobiega15mo ago
Thats a curious way to spell Serilog
Azrael
Azrael15mo ago
Nuh uh. You're bullying me. I don't like it.
Pobiega
Pobiega15mo ago
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
jcotton42
jcotton4215mo ago
now I'm curious why Serilog is great
Azrael
Azrael15mo ago
Why would i enjoy getting bullied? Why don’t you like NLog?
jcotton42
jcotton4215mo ago
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"
Azrael
Azrael15mo ago
I like NLog because it allows me to easily log them in a file.
jcotton42
jcotton4215mo ago
Did you mean "to a file"?
Azrael
Azrael15mo ago
Yes. I just woke up.
jcotton42
jcotton4215mo ago
Serilog also has a file sink
Azrael
Azrael15mo ago
I've never tried Serilog, how would one go about setting up a file sink?
jcotton42
jcotton4215mo ago
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 ...
Azrael
Azrael15mo ago
It's like NLog.
jcotton42
jcotton4215mo ago
Does NLog do structured logging?
Azrael
Azrael15mo ago
Serilog does seem to have more flexibility with their options. Like?
jcotton42
jcotton4215mo ago
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.
Azrael
Azrael15mo ago
JSON formatted data?
jcotton42
jcotton4215mo ago
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...
jcotton42
jcotton4215mo ago
That's one way to represent structured data
Azrael
Azrael15mo ago
Yes. NLog has that. This is just an interface reading from structured data.
jcotton42
jcotton4215mo ago
Ik, but it was just an easy way to show what you can do with structured logging
Azrael
Azrael15mo ago
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.
Aljo_M
Aljo_M15mo ago
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
Aljo_M
Aljo_M15mo ago
Shiv
ShivOP15mo ago
Thank you all for the inputs.. I will explore serilog , can we write logs to sqlserver db using serilog ?
jcotton42
jcotton4215mo ago
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
jcotton42
jcotton4215mo ago
A dedicated log store like Seq is a better fit
Accord
Accord15mo ago
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.
Want results from more Discord servers?
Add your server