log in asp.net mvc - asp.net-mvc

In my mvc application i need to check a condition in action and throw it to the log.
How can i add a info to log info.

To use the event log:
System.Diagnostics.EventLog.WriteEntry()
Or look at a library like log4net or ELMAH.
Kindness,
Dan

Have you tried using EMAB or Log4Net libraries?
Yes...there are built-in classes but instead of building it ground-up the best method is to use established frameworks.
Check this out >>> MS Logging Application Block
And if your needs are at a very basic level the System.IO.Log should suffice.
For unhandled errors ELMAH is the easiest to use.

Related

Does ELMAH provide any methods to READ the errors from the database?

I am working with ASP.Net C# application and using ELMAH to log errors to SQL database.
I need to retrieve the errors from the database, does ELMAH provide any methods to search an error (and return something such as JSON)? Or does ELMAH only logs the errors, and reading part has to be donelike any other database read?
I have checked online and at https://elmah.github.io/ . I could not find any lead to the solution of this issue.
Thanks in advance for any answers!
ELMAH provides a simple UI for browsing errors, available on the URL /elmah.axd (or /elmah if you are using the elmah.mvc NuGet package). Searching isn't supported, but you can export errors from the UI.

ELMAH --Is There a way to identify from Error log Unhandled or userHandled?

Am using ELMAH and want to know if the Error reported by ELMAH is Unhandled or it is handled in code and logged
ErrorSignal.FromCurrentContext().Raise(context.Exception);
Is there a way to identify it ?
Might be a simple but just curious to know
Thanks
Instead of using ErrorSignal.FromCurrentContext().Raise I use ErrorLog.GetDefault().Log. There is a nice example in log4net elmah appender source code.

Elmah.MVC vs. Elmah.contrib.Mvc

I'm new to ASP.NET MVC and I'm looking for the least painful way to get global error handling, logging, and reporting (via email) set up. FYI, my ASP.NET MVC app is being hosted as a web role in Azure, but I'm trying to avoid using any Azure-specific hooks.
It looks like getting ELMAH installed and configured is a good first step.
When I look in NuGet, I see:
ELMAH ("ELMAH with initial configuration for getting started quickly...")
Elmah.Contrib.Mvc ("...designed to add ease-of-use inside MVC projects...")
Elmah.MVC ("...painless integration into MVC...")
Which of these packages do I want to install?
EDIT: After some experimentation, I'm more confused. I compared the project files after installing each of the three options on top of a default MVC 4 app. It appears that neither Elmah.Contrib.Mvc nor Elmah.MVC add the elmah.1.2.2 package (which I get when I install basic ELMAH). Also, neither package adds the <elmah> section to Web.config.
So... do I need to install ELMAH first, and then install one of the "...MVC" packages to get whatever nifty improvements they presumably get me?
ELMAH - This is the default ELMAH package with no mvc specific code in it, if you dont do what is described here I don't believe ELMAH will log any errors that occur in you're controllers, and you may not be able to access the ELMAH log page
Elmah.Contrib.Mvc - This is an enhancement on ELMAH specifically for MVC based upon one of the creators of ELMAHS answer to a stack overflow question.
Elmah.MVC - This is simliar to the above package however I believe it provides much better support for mvc features like routes etc, It is also quite easy to install and configure, it removes a lot of the messing around you would have to do with ELMAH to take out various parts that arent required when running in a mvc project
You can read the authors posts to get a better idea of how it works:
Integrating ELMAH to ASP.NET MVC in right way
ELMAH MVC controller released on NuGet
ELMAH.MVC v.2.0.0 - Release Candidate
Personally I believe the last package would be the best one ot use, it seems to be the more up to date and maintained of the two mvc ones.
You might consider using Elfar instead of Elmah. Elfar is "inspired" by Elmah but is designed to be MVC specific and is very simple to configure.
Just open NuGet, and search for Elfar. You will find a number of different packages, but in general you want the one that conforms to the technology you are intested in using. I'm not familiar with Azure web roles, but if you have access to a standard Sql Server then you can simply use Elfar.SqlClient.
More info here:
https://github.com/stevenbey/elfar/wiki

ASP.NET MVC web application exception handling

ASP.NET MVC web application exception handling using application blocks.
How can i configure Microsoft Application blocks for exception handling in .NET MVC2 web application logging the error to the sql database?
You can write own Library bur this is not easy so i prefer u Use Elmah
I think elmah is good because now 588,786 is downloaded so we safely to use.
You can simply install
PM> Install-Package elmah
ELMAH with initial configuration for getting started quickly. ELMAH
(Error Logging Modules and Handlers) is an application-wide error
logging facility that is completely pluggable. It can be dynamically
added to a running ASP.NET web application, or even all ASP.NET web
applications on a machine, without any need for re-compilation or
re-deployment.
You might want to think about ELMAH. It is much lighter than Application Blocks and you can use it as a filter for handling errors.

How can you get the "real" HttpContext within an ASP.NET MVC application?

Unfortunately, I need to do this. I'm using ELMAH for my error log. Before I route to my error.aspx view, I have to grab the default ELMAH error log so I can log the exception. You used to be able to use
Elmah.ErrorLog.Default
However, this is now marked as obsolete. The compiler directs me to use the method
Elmah.ErrorLog.GetDefault(HttpContext context)
MVC's context is of type HttpContextBase, which enables us to mock it (YAY!). How can we deal with MVC-unaware libraries that require the old style HttpContext?
Try System.Web.HttpContext.Current. It should do the trick.
Gets HTTP-specific information about an individual HTTP request.
MSDN
this.HttpContext.ApplicationInstance.Context

Resources