Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices for logging in ASP.net MVC?

What's the best way to log in ASP.net MVC? I mean any event, I'm currently using NLog but I know there are a lot of possible ways to do it.

like image 652
ryudice Avatar asked Dec 05 '09 02:12

ryudice


People also ask

What is logging in ASP NET MVC?

In this article, we are going to learn how to implement the best Logging libraries available in the market for an ASP.NET MVC application. What is Logging? Logging is the act of keeping a log. The log file is a file where all events or error of application is written.

What is the logging provider for ASP NET Core?

The logging provider is included as a dependency of Microsoft.ApplicationInsights.AspNetCore, which is the package that provides all available telemetry for ASP.NET Core. If you use this package, you don't have to install the provider package. The Microsoft.ApplicationInsights.Web package is for ASP.NET 4.x, not ASP.NET Core.

What are some good practices for building ASP NET MVC applications?

Good practices for building ASP.NET MVC applications include: enforce good project folders and namespaces as you move from project to project; use bundl... Home

How do I write logs to files from an ASP core app?

ASP.NET Core doesn't include a logging provider for writing logs to files. To write logs to files from an ASP.NET Core app, consider using a third-party logging provider.


2 Answers

I use log4net, its quite good. There are some issues to be aware of, you can learn more about them here. I also recommend Elmah, for me I use it on every project I do, its a prerequisite.

like image 161
Lukasz Avatar answered Oct 08 '22 23:10

Lukasz


I don't think there is a best framework/tool or standard way in ASP.net MVC. Just do it the way you would in any other framework. When I set up logging, I usually think of it as a resource available to the rest of the application, rather than being tied to a particular tier. This is common, and in fact logging is the standard example given when introducing Aspect Oriented Programming. See:

  • Logging mentioned in the wikipedia entry on AOP
  • Another AOP into that uses logging as the example

Depending on what exactly you're trying to log, consider using action filters; a great way to log what page requests are made and for error handling coverage. Non-MVC asp.net apps usually do something in the global.asax, as mentioned here. In fact, even if you use the action filters, which I would suggest, also include some basic error handling in the global.asax 's application_error event; it will fire a little more dependably than the action filters if something really crazy happens.

Other than that, call your logging resource at the point where the stuff happens that's interesting to you. DB or File? Either works, and as long as it's encapsulated in a good method or two, you can always switch that later.

like image 34
Patrick Karcher Avatar answered Oct 09 '22 00:10

Patrick Karcher