2

I have been trying to write Logs(Trace, Information & Exception) in Azure AppInsights using Log4Net instead of default api Telemetry client. When I run the application from VS2013 neither I get any error message nor am seeing logs in Azure portal.

Pleaes help me figure out this issue.

Note: Am using Log4net appender for AppIinsights. Web.Config

 <log4net>
<root>
  <level value="ALL" />
  <appender-ref ref="aiAppender" />
</root>
<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message%newline" />
  </layout>
</appender>

MVC Controller

public class HomeController : Controller
{

    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    public ActionResult Index()
    {
        //Trace.TraceInformation("Home accessed at : {0}", DateTime.UtcNow);
        Log.Info(string.Format("Home accessed at : {0}", DateTime.UtcNow));
        return View();
    }
}

Regards, Rajaram.

2 Answers2

0

If you are not seeing any log4net output, i'm presuming you are missing some log4net startup code, like this:

log4net.Config.XmlConfigurator.Configure();

which you might want in your startup class / code somewhere. Without that, log4net doesn't know wo read the configuration that's in web.config.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
0

In addition to the answer from @JohnGardner, you can instead add a line to your AssemblyInfo.cs file as so: -

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

There is more discussion on the two approaches in the following question: -

Configure Log4Net in web application

And in a comment in somewhere in that discussion is a link to the log4net FAQs that touches on the differences in the question "When should I log my first message?": -

https://logging.apache.org/log4net/release/faq.html#first-log

I found both of these to be of further use to me.

Community
  • 1
  • 1
Wayne Birch
  • 645
  • 6
  • 18