Where do I use ILog in ASP.NET? - asp.net-mvc

I'm working in a MVC project, which has several controllers. Each has in the beginning
private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
In c#, I was using loggers in exception. I can't understand why each controller has its own logger.

hi it looks like log4net declaration. its a tool to log your error in all kind of .net program like web app ,console, windows etc. so that exceptions are logged and developers know what exactly gone wrong am mentioning link for a tutorial if you want additional information on matter.
https://www.codeproject.com/Articles/140911/log-net-Tutorial

Related

The field, constructor or member 'MapRoute' is not defined in F# Web API Project

I'm taking my first steps in F# development and I've ran in the following issue; I'm trying to setup the routing of an web api and when I try using the MapRoute method of a RouteCollection I get the following error:
The field, constructor or member 'MapRoute' is not defined
For the project I used the F# MVC 5 templates from Daniel Mohl, I've tried using Web API nuget packages and referencing System.Web.Mvc where the RouteMap extensions seems to reside. I've read that extensions methods (which MapRoute seems to be) can cause problems, but I've no clue how to fix it. Also I saw suggestions loading references in the Interactive window, but that didn't work either (seems not surprisingly). My questions is now what is the best approach here in creating a pure F# Web API project? (I've also read this http://blog.ploeh.dk/2013/08/23/how-to-create-a-pure-f-aspnet-web-api-project/, but it seems a bit outdated) Sub questions how do I solve errors like this in the future?

What is your opinion on using MVCExtensions with Autofac

I would like to know your opinion on using MVCExtensions with Autofac..
I am using MVC Extensions with Autofac in my MVC 3 web app. I'm just not getting it why these extensions were created? I can't really say that it is makng my life easier. Everything that I did with Autofac in my global.asax.cs file I can do with the MVC extensions. I can register routes, controllers, my services and repositories, etc with just Autofac.
Any opinions why using these MVC extensions is maybe a better way to go?
I need to capture my errors in Application_Error. How do I get an instance of my registered logger? If I need to resolve dependencies some where else in my app how would I do this? This is how I register my logger:
builder.RegisterType<Logger>().As<ILogger>();
I can instantiate my logger but would rather like to use the registered logger.
Is there a tag for these extensions?
You should be able to access your logger through the DependencyResolver:
var logger = DependencyResolver.Current.GetService<ILogger>();

How To Properly Configure Ninject.Extensions.Logging.Log4Net in my MVC3 project

I am trying to properly use Ninject to inject log4net logging into my MVC3 application. I am using the Ninject.MVC3 package, so I have the NinjectMVC3 class that automatically extends the App_Start method and contains the RegisterServices method that binds all dependencies. I also have the Ninject.Extensions.Logging.Log4Net package, but I don't know how to use it. I already know how to configure log4net in my web.config, but don't know how to use this extension for DI.
I have read all the following articles/posts, but none of them seem to define how to properly setup a project for DI logging.
At http://dotnetdarren.wordpress.com/2010/07/29/logging-in-mvc-part-4-log4net/, Darren
provides a great article, but doesn't seem to deal with DI (at least I don't see it).
At Using Ninject to fill Log4Net Dependency,
Remo Gloor states here that the extensions should provide all that's needed for implementation, but it doesn't show the code of how to instantiate it.
The documentation for ninject.extensions.logging at https://github.com/ninject/ninject.extensions.logging/wiki/Using is very limited at best. I have re-read it many times, and still don't see how to use bind the injection in the NinjectMVC3 class, or concrete examples of how to call the logger from my controller class for example.
At the most promising article, Moosaka provides some great code at Ninject.Extensions.Logging.Log4net unexpected behavior, but when I try it, I get a compile error in the LoggerFactory at ILogger logger = new Logger(type); stating "Cannot access protected constructor 'Logger' here". Also, he states to "Tuck this whole mess away into a separate class library". Does that mean as a whole separate project?
I'm just getting lost in all the differing options and dated posts and would like any input on how to use Dependancy Injection with Ninject and Log4Net in my MVC3 project. Also, if it matters, all of my Ninject code is in my domain project, but the logging needs done from both the domain and web project (and mocked in my unit tests). Any help is appreciated.
You shouldn't have to configure anything except the normal log4net config.
All you have to do is to inject a ILogger wherever you want to log.
https://github.com/ninject/ninject.extensions.logging/wiki/Using

log in 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.

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