30

I want to record software process logs to files. Rather than make my own log system, I am trying to use Log4Net with ASP.NET MVC, but I have run into problems setting it up in Visual Studio 2015, like:

  1. How to setup web.config / Global.asax page?

  2. How to install components in VS 2012 ~?

  3. How to use it in my *.cs file?

What are the steps to properly configure Log4Net With ASP.NET MVC C# in Visual Studio 2015?

I also wrote a Q&A to set it up for ASP.NET WebForms, see How to use Log4net from Nuget with Visual Studio platform in the ASP.NET Web Form (Easy method).

0

1 Answer 1

87

Step1: To use the Nuget to get the log4net package:

enter image description here

Step2: tell log4net to initialize itself from the XML configuration (Web.config), by adding this call in the Global.asax.cs file under Application_Start():

log4net.Config.XmlConfigurator.Configure();

enter image description here

Step3: add the configuration section in Web.config between tag <configSections>...</configSections>:

  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

enter image description here

Step4: Insert the actual log4net configuration <log4net>...</log4net> (Within <configuration>...</configuration> but after the </configSections> tag) , see Apache log4net™ Config Examples for more examples:

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

enter image description here

Now you're ready to make calls to an ILog to write actual log statements to the configured appender(s):

ILog log = log4net.LogManager.GetLogger(typeof(HomeController));      

public ActionResult Index()
{
    log.Debug("Debug message");
    log.Warn("Warn message");
    log.Error("Error message");
    log.Fatal("Fatal message");
    ViewBag.Title = "Home Page";
    return View();
}

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Is there an impelling reason for that static "Log" member? The way I see it is that all you need is the non-static member "log"...
@WillieCheng My comment was about the fact that while using a static member is IMHO the most beneficial choice, your code uses a non-static instance to perform every action ("ILog log" is clearly non-static, while your static Log is never initialized). I would have done something like: "private static ILog Log {get;}=LogManager.GetLogger(typeof(HomeController));" and remove your second declaration.
+1 great answer, saved a lot of my time, just a little enhancement for devs: if you want to show the logged messages in the Visual Studio Output while debugging, use the TraceAppender from the apache log4net config exmaples link provided in the article :) ConsoleAppender did not worked for me.
@marco6 most people who use log4net use static to keep track of what class or method called the logger. They all start off with private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); as the start of every class. use stackify.com/log4net-guide-dotnet-logging for reference, specifically he says 'Declare it as static and use this little trick so you don’t have to hard code the class type.'
@Jean-Paul agreed! That is exactly what my comment was about! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.