Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NLog Setup

Posted on 2007-11-20
3
Medium Priority
?
2,536 Views
Last Modified: 2009-07-16
I would imagine that alot of people here have setup NLog in their own projects so I will ask here before I go over to the NLog forum.

I am trying to setup NLog for my code.

My NLog.Config is:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="console" xsi:type="ColoredConsole"
            layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}"/>
    <target name="file" xsi:type="File" fileName="${basedir}/file.txt"
            layout="${stacktrace} ${message}"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="console,file"/>
  </rules>
</nlog>

While within my code I do:


private static Logger logger = LogManager.GetLogger("MyClass1");

public MyClass1()
{
  logger.Trace("Some Log Message");
}

public void SomeMethod()
{
  logger.Trace("Some Method Message");
}


The problem appears to be that the .Config file is not being used. Thus the .txt file is never used yet if I do all the setup in the constructor there is no problem:

// Step 1. Create configuration object
         
        LoggingConfiguration config = new LoggingConfiguration();
         
        // Step 2. Create targets and add them to the configuration
 
        ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
        config.AddTarget("console", consoleTarget);
         
        FileTarget fileTarget = new FileTarget();
        config.AddTarget("file", fileTarget);
         
        // Step 3. Set target properties
         
        consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
        fileTarget.FileName = "${basedir}/file.txt";
        fileTarget.Layout = "${message}";
         
        // Step 4. Define rules
         
        LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
        config.LoggingRules.Add(rule1);
 
        LoggingRule rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
        config.LoggingRules.Add(rule2);
         
        // Step 5. Activate the configuration
 
        LogManager.Configuration = config;


But I would rather rely on the .Config files.

Any help getting it working with the config files would be great.


I've been using:
http://www.nlog-project.org/tutorial.html
0
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
wizrr earned 2000 total points
ID: 20319268
You can use NLog.Config.XmlLoggingConfiguration. This is also logging configuration (iherited from LoggingConfiguration class). You can use it to load config from file. You also can put nlog configuration to your application config file using config file section's handlers. See example, there are little explanations:
NLog.Config.XmlLoggingConfiguration conf = new XmlLoggingConfiguration("nlog.config");
// or in your app.config\web.config file:
<?xml version="1.0"?>
<configuration>
	<configSections>
        <!-- as an sample there can be another config sections with another purpose-->
        <sectionGroup name="spring">
			<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
			<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
		</sectionGroup>
        ...
        <!-- add section handler to your config, this class is used by configuration system to know how configuration section must be loaded-->
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog, Version=1.0.0.505, Culture=neutral, PublicKeyToken=5120e14c03d0593c"/> <!-- be sure Version and Public Key is same as your nlog.dll -->
        ...
    </configSections>
       ...
    <!-- after that define your section-->
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
		<targets>
			<target name="targetfile" xsi:type="File" fileName="${basedir}/Logs/log.${level}.txt" layout="${date} ${level} ${message} ${newline} ${exception:format=ToString} ${newline}"/>
		</targets>
 
		<rules>
			<logger name="*" minlevel="Trace" writeTo="targetfile"/>
		</rules>
 
	</nlog>
        ...
        <spring> <!-- this is section group as defined after "configSections". you can put your nlog config everywhere-->
            <objects/>
            <context/>
        </spring>
</configuration>

Open in new window

0
 

Author Comment

by:directxBOB
ID: 20319970
I put the above in my web.config is that all I need to do? as the log files are still not coming out, so I am just trying to narrow down what I should be going after. Either the web config is wrong, my class is wrong or both.
0
 
LVL 3

Expert Comment

by:wizrr
ID: 20320101
You should only use this part of xml:

<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog, Version=1.0.0.505, Culture=neutral, PublicKeyToken=5120e14c03d0593c"/>

and this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
                <targets>
                        <target name="targetfile" xsi:type="File" fileName="${basedir}/Logs/log.${level}.txt" layout="${date} ${level} ${message} ${newline} ${exception:format=ToString} ${newline}"/>
                </targets>
 
                <rules>
                        <logger name="*" minlevel="Trace" writeTo="targetfile"/>
                </rules>
 
        </nlog>

I provided another parts for example only.
Try use this code:

NLog.LogManager.ThrowExceptions = true;

If there any errors occured when writing log files NLog.LogManager.ThrowExceptions will be tested and if NLog.LogManager.ThrowExceptions == true exception will be thrown. Maybe you use NTFS file system, and your IIS user has no permissions to write to files. You should create Logs folder and change permissions of IIS user account to allow write\create files.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question