Abhishek Jain
Abhishek Jain

Reputation: 315

How to log error from presentation layer to data layer

I want to log exception in my project through global.asax file into database but from my presentation layer I won't be able to access the data layer because my presentation layer communcicates with data layer via webservices, so my question is should i create service to log exception to my database.

this is my architecture enter image description here

Upvotes: 1

Views: 754

Answers (2)

Jonas Høgh
Jonas Høgh

Reputation: 10884

It depends on what you mean by errors in the presentation tier. Errors in the server side code, e.g. WebForms code behind? Or errors in the user's browser? If the latter, you obviously need some network-based service (there are many third party products for collecting errors in browsers by embedding a bit of JavaScript)

As for your server side components, don't think of logging as a mechanism of your data layer just because it writes to a database. Think of it as a cross cutting concern that just happens to be implemented via a database at the moment. I would simply use a standard logging library such as Log4Net or Serilog for this across the layers, not attempt to wrap it in your own abstractions within the data layer.

You also generally don't want to use exactly the same persistence configuration for logging as for data access in general. For instance logging should often be performed to a separate database for operational reasons such as backup policies. Enlisting your log writes in the same transaction management mechanism as your business data access is also a recipe for accidentally rolling back writes to the log when an error occurred, leaving without any knowledge of the error.

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151730

Should I create a service to log exception to my database?

No.

If an error occurs in your web service, log it in your web service. Don't propagate that error to the UI and expect the UI to report that error back to the same or another web service.

It's another story if you want to report errors that occur in your UI. You could opt to report such errors through a service, but what if the error is network-related and the log service is unreachable? Rather log as local to the application as you can.

Upvotes: 1

Related Questions