Hi,
I am looking for a simple way to log exceptions to the database in my ASP.NET MVC application. I have looked at the "Exception Management Application Block" but I canĀ“t find any simple and clear articels about how to handle this in ASP.NET MVC?
Maby I should just catch the exception as far up as possible and then log it to database but Im not sure how to do that in ASP.NET MVC. In WindowsForms there is diffrent events(like unexpectedException) to listen to where I can log, is there anything like that in ASP.NET MVC?
BestRegards
Edit1: I found a tool called elmah http://code.google.com/p/elmah/ but im not sure if this is a good solution and if it works well with ASP.NET MVC.
Then I also found this article http://www.davidjuth.com/asp-net-mvc-error-handler.aspx that looks easy and clear but I also do not know if this is the right way to go?
Use ELMAH, it is available on NuGet. It will log unhandled exceptions in a web app and provide an interface to view them. It can log to a variety of storage mechanisms including xml and sql databases, it can also email you errors. It is basically the solution for logging exceptions in ASP.Net applications (both WebForms and MVC).
If you are using ELMAH in an MVC app, there are some integration points that should be addressed. These can be handled pretty well in one shot by installing the ELMAH.Mvc package from NuGet.
I would suggest you have a look at NLog for logging exceptions: http://nlog-project.org - you can configure it to dump the exceptions into a database really easily.
As for MVC error logging - this question Error Handling in ASP.NET MVC has some really good solutions in it.
Related
I'm moving stuff from my ASP.Net MVC application (.Net Framework 4.7) to a new .Net Core 2.2 API.
In my old MVC application I added some specific POST properties to the IIS logs via HttpResponseBase.AppendToLog(). I can't find a similar method in .Net Core.
My question: how can I add properties that are in the HttpPost to the IIS logs in a .Net Core Api? This is how the logs look like:
2019-06-26 19:34:07 website-ip-address POST /some-endpoint [data-to-log-ended-here] 443 username ip-address etc.
I don't have a direct answer to your question, but I found two posts that may relate to what you are asking
How to log http POST data to IIS log in asp.net core
Where do logs go when running ASP.NET Core on IIS 7.5?
Generally, in an Asp.net Core app it's more common to use its logging API to work with a powerful 3rd party logging libraries like Serilog to easily log to a number of different sources you can choose from. Though IIS is not one of them, I'd recommend take a look at Application Insights which is very popular for doing logging in Asp.net Core applications. Hope this helps somewhat!
I implemented Serilog for a ASP.NET MVC project which will be hosted in Azure and Serilog provides sinks to log to Azure storage.
I then wanted a better way to handle exceptions and ran into this very informative article on integrating ASP.NET MVC with Elmah - http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx
I like it that Elmah lets you view exceptions and has ways to notify admins when exceptions occur. Given that I already have Serilog, should I replace it with Elmah or use it in conjunction with it?
Elmah is only used to log unhandled exceptions. That is, exceptions that would otherwise result in a yellow screen of death. It has a very nice log viewer as well, but ultimately, it's only used for exception logging.
Serilog can do everything that Elmah can do, with the exception (no pun intended) of the built-in viewer. However, there are lots of ways to view your exceptions.
Serilog will also do tracing or "event logging", which Elmah won't do (by default, there are ways to use Elmah's infrastructure to do this).
Finally, you don't get the structure of Serilog's logging with Elmah. You just get flat logfiles.
You can use both if you want, but I'd rather just configure an Exception handler to log to Serilog.
FYI, Serilog can log to Elmah.
http://blog.elmah.io/logging-to-elmah-io-from-serilog/
There's a good blog entry on the Elmah website about the difference between Elmah and Log4net. Many of the same reasons apply with Serilog, although obviously there is also Serilog's structured logging which you wouldn't get with either of those.
http://blog.elmah.io/elmah-vs-log4net/
Also, despite the fact I've linked to elmah.io, don't be confused. There are two versions of Elmah. One of them is free (and open source), the other is not (although it is partially open source). Elmah.io is cloud based, and not free. Elmah is still open source and free.
http://code.google.com/p/elmah/
Can anyone give me a brief description of owin?
What is its relationship between asp.net mvc external log in?
and relation to Microsoft.Owin.Security.Google?
You can read about OWIN at http://owin.org. That should give you some understanding about the structure and interface of the framework and how it is used to separate any midware, e.g. MVC from a concrete server process e.g. IIS. Microsoft wants to adopt this framework/interface to make middleware like MVC available on more systems. Currently (MVC5) MVC is not yet a OWIN middleware, but parts like authentication already are. So this is why you have some assembly like Microsoft.Owin.Security.Google referenced in your project.
I have an SQL Server database with a MVC 4 internet application. How do I use OAuth with my existing database and existing user table (unfortunately called 'Customer'). I'm trying to avoid changing as much as I can. Does anyone have any helpful information?
Take a look at DotNetOpenAuth. It's shipped with VS2012, is available via NuGet, and seems to be geared more toward the ASP.NET MVC audience than simpler WCF setups.
Following code throws (handled) exceptions, it appears that MVC uses exceptions to test for control locations.
<% Html.RenderPartial("LogOnUserControl"); %>
Application works as expected, I can see that exception is thrown couple times trying different locations (Views, Shared). Same thing happens with other controls. Apparently MVC uses exceptions to probe different possible locations for the file.
IIRC using exceptions for flow control is evil, and is not cool.
So, am I doing something wrong, or MVC is cool no more?
Note: having IDE stop on all thrown exception makes debugging easier, and I normally leave it on. That's how I got to that exception from RenderPartial.
It is not true that MVC 2.0 uses exceptions for control flow.
However, System.Web.dll v2.0 (the core component of ASP.NET up to .NET 3.5) has some inefficient APIs for instantiating objects from virtual paths. MVC 2.0 mitigates this problem by having a cache of view lookups. By default this cache is disabled during development so that the changes you make are immediately visible, which is why you are seeing these exceptions. On a real production server these exceptions would not occur after the lookups are cached.
As a side note, MVC 3 will be using new APIs added in .NET 4 so this should not be a problem anymore.
When running in Release mode view locations are cached.