3

Can I use NLog in a WCF Service? I am trying to but cannot get it to work.

First I set up a simple configuration in a Windows Forms application to check that I was setting up correctly and this wrote the log file fine (I am writing to a network location using name and not IP address).

I then did exactly the same thing in the WCF Service. It did not work.

To check permissions I then added some code to use a TextWriter.

        TextWriter tw = new StreamWriter(fileName);
        tw.WriteLine(DateTime.Now);
        tw.Close();

This worked OK so I know I can write to the location.

2
  • Try turning on NLog internal logging by modifying the first lines of your NLog configuration to look like this: <nlog xmlns="nlog-project.org/schemas/NLog.mono2.xsd" xmlns:xsi="w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Warn" internalLogFile="nlog_log.log" > Turn the level down as far as Trace and see what it tells you.
    – wageoghe
    Commented Feb 3, 2011 at 1:02
  • Formatting not so good on that comment. See "Treating exceptions differently" in this thread for a good example of how turn on internal logging for NLog. stackoverflow.com/questions/4091606/…
    – wageoghe
    Commented Feb 3, 2011 at 1:05

3 Answers 3

1

Check that your NLog.config file is in the same directory as your .svc file and NOT the Bin directory.

If you've just added the config file to the WCF project, then published it you will probably find your config file has been copied to the bin directory which is why NLog can't find it. Move it to up a level then restart the website hosting the service (to make sure the change is picked up).

This had me stumped for a while this morning!

0
0

Put your NLog config in the web.config file. Like so:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>

  . . . (lots of web stuff)

  <nlog>
    <targets>
      <target name="file" xsi:type="File" fileName="${basedir}/logs/nlog.log"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="file" />
    </rules>
  </nlog>
</configuration>
0
0

See my comment to your original question for how to turn on NLog's internal logging.

To turn on NLog's internal logging, modify the top of you NLog config to look like this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.mono2.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
      autoReload="true"       
      internalLogLevel="Trace"       
      internalLogFile="nlog_log.log"       
> 

The key parts are internalLogLevel and internalLogFile.

You can also set internalLogToConsole to true or false to direct the internal logging to the console.

There is another setting, throwExceptions, that tells NLog whether or not to throw exceptions. Ordinarily, this is set to false once logging is successfully configured and working. You can set it to true to help determine if your problem is due to an NLog error.

So, if you had all of those options enabled, the top of your NLog configuration might look like this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.mono2.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
      autoReload="true"       
      internalLogLevel="Trace"       
      internalLogFile="nlog_log.log"       
      internalLogToConsole="true"
      throwExceptions="true"
> 

My first guess is that NLog is not finding the config information. Are you using an external config file (NLog.config) or "inline" configuration (in your app.config or web.config)? In your project, is(are) your config file(s) marked (in Properties) as Copy Always?

1
  • All set up in NLog.config as you have suggested. Rest of file is... 'code'<targets><target name="file" xsi:type="file" layout="${longdate} ${message}" fileName="\\<server>\logfile.txt" keepFileOpen="false" encoding="iso-8859-2"/></targets> <rulse><logger name="*" minLevel="info" writeTo="file"/></rules>'/code' I get no errors and no indication of a problem but also no log file.
    – EzaBlade
    Commented Feb 6, 2011 at 12:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.