Enabling Logging with NLog

I’ve enabled logging in my project with NLog and it’s working fine, I can produce output from my application.

I’ve tested a job in hangfire which produced an exception like

"System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation."

Nothing appeared in my logs for this. Should I expected to see a log entry from hangfire for such an exception?

I have nlog configured to capture all messages i.e.

  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>

I thought this would just work based on the documentation?

Thanks in advance,

Tom

Oh, sorry for the absent answer! :cry: Yep, documentation should be correct, but looks like it doesn’t. I’ll try to look at this issue tonight.

Thanks for following up, I look forward to your response!

Please tell me exact versions of the following packages you use and the full stack trace of your exception.

  • NLog
  • Common.Logging
  • Common.Logging.NLog

I’ve tried to install NLog support for the project using steps written in the documentation (but changed the Common.Logging.NLog20 with Common.Logging.NLog) with the following versions, and all is working fine for me.

  • NLog 3.0.0.0
  • Common.Logging 2.1.2
  • Common.Logging.NLog 2.0.0

So, I was having a really hard time getting this working.
I couldn’t get the configuration of the logging adapter to work as per the documentation, I kept getting errors when trying to declare the LogManager.Adapter.

I ended up moving my config into the web.config file and still had problems getting the project to run.

In the end, the problem was the installation of the nuget package.
I uninstalled Common.Logging.NLog and installed Common.Logging.NLog20

This seems to be a known dependency issue, people can read more about it here;

You should probably change the documentation reference for the Install-Package command.

Thanks for your help!

Unfortunately Common.Logging.NLog20 does not work with Common.Logging 2.2.0 (the latest stable version), because version 2.2.0 contains breaking changes :frowning:. You have problems with Common.Logging.NLog package, I had problems with Common.Logging.NLog20 package.

In Hangfire I tried to claim the lowest possible dependency version (Common.Logging 2.1.2, Newtonsoft.Json 5.0.1, etc.) for easier integration into existing projects without updating dependencies (that can cause breaking changes). But tried to choose only references to those versions that allows Hangfire to work after an upgrade to the latest stable.

Currently both 2.1.2 (referenced) and 2.2.0 (latest) versions of the Common.Logging package contain different problems regarding to a version. To prevent the configuration burden, I’ll update Hangfire to claim the latest, 2.2.0 version of Common.Logging, so everyone will have the same problems :).

Interesting. I’m using

  • Hangfire 0.9.1
  • Common.Logging 2.2.0
  • Common.Logging.NLog20 2.2.0

Everything is working fine for me! I won’t do anything to disturb this for now :slight_smile:

And I’ll check the Common.Logging 2.1.2 with Common.Logging.NLog20 once again. If it does not work, then I update the reference to Common.Logging 2.2.0 package.