0

I have`ASP.Net WebAPI based project & I am using Log4Net to log exception. Here is Exception Handling stuff.

[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
    private ILogger _logger = NullLogger.Instance;

    public HttpResponseMessage Get(int id)
    {
       try
       {
          // my stuff
       }
       catch(Exception ex)
       {
            LogException(ex);
       }
    }
}

private void LogException(Exception ex)
{
    var msgParams = new Dictionary<string, string>
    {
        { "Message", ex.Message },
        { "StackTrace", ex.StackTrace },
    };
    _logger.Info(JsonConvert.SerializeObject(msgParams));
}

DI Configuration.

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig("log4net.xml"));
}

My Web.Config

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>

  <dependentAssembly>
    <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
  </dependentAssembly>
</assemblyBinding>

Log4Net.xml

<?xml version="1.0" encoding="utf-8" ?>

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

    <file value="App.log" />

    <appendToFile value="true" />

    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="2" />

    <encoding value="utf-8" />

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %thread %logger - %message%newline" />
    </layout>

</appender>

<root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFile" />
</root>

But when any exception occurs & controls enters the catch block neither any log file is created nor the exception is logged.

What I am missing here??

Any help/suggestion highly appreciated.

3
  • You have not shown your castle initialization and registering the different objects - more relevant than the all the assembly binding Commented Apr 4, 2017 at 15:10
  • 1
    I'm gonna go out on a limb here, and while I know nothing about Log4Net, and say that the NullLogger class looks awfully suspicious... Commented Apr 4, 2017 at 15:10
  • add to log4net node in your xml debug=true, that might give you some clue. Commented Apr 5, 2017 at 13:13

1 Answer 1

3

Your logger is private and you do not inject this dependency via the constructor. For Castle to inject it via Property Injection it should be public.

You can see more in the documentation. Castle Windsor's recommendation as a best practice:

private ILogger logger = NullLogger.Instance;

public CustomerService()
{
}

public ILogger Logger
{
   get { return logger; }
   set { logger = value; }
}
Sign up to request clarification or add additional context in comments.

10 Comments

If that's the cause then I should get Null Exception
But in my case neither I am getting any exception nor it is logging any exception
@Kgn-web - no you won't because a NullLogger isn't null. It's a logger that does nothing. A nice way for Castle to enable you to just call the logger without worrying if it is actually set (Because usually loggers are a side business and a class's weak dependency)
@Kgn-web - If you debug your code you will see that the logger is called and if you check you will see it is not null but a proper object - one can check the implementation but it probably just has empty implementations for the functions
Let me check again. However I have added MY DI stuff in the post. Please have a look
|

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.