Trying to configure File target at runtime, what am I missing?

4,843 views
Skip to first unread message

snafu

unread,
Oct 19, 2012, 12:53:51 PM10/19/12
to nlog-...@googlegroups.com
I have a scenario where I have several custom windows services running and I want to be able to dynamically name my log file path according to the name of the service on startup. So first I created a very simple target like this:

<target name="MyTarget" xsi:type="File" createDirs="true" fileName="C:\Test\Logs\log.txt" archiveEvery="Day" archiveNumbering="Rolling" layout="${message}" />


where I set the fileName property (required) to the base location I want to use. Then at startup I call this method:

private static void SetLoggerFileName(string serviceName) {
     
NLog.Targets.FileTarget target = LogManager.Configuration.FindTargetByName("MyTarget") as NLog.Targets.FileTarget;
     
string path = target.FileName.ToString();
     path
= path.Substring(0, path.LastIndexOf(System.IO.Path.DirectorySeparatorChar));
     target
.FileName = System.IO.Path.Combine(path, string.Format("{0}.txt", serviceName));
}


But it doesn't create the log file nor does it throw any exceptions or have anything in the internal nlog logging. If I comment out the call to SetLoggerFilename then it works as expected and the log.txt file is created and logged to.

I must be missing a step here or something, do I have to tell nlog that its config has changed or something?

thanks
Message has been deleted

snafu

unread,
Oct 20, 2012, 10:32:00 AM10/20/12
to nlog-...@googlegroups.com

I'd still like to know why this doesn't work, but I found a work around using custom properties and the event-context...changed my target to this:


<target name="MyTarget" xsi:type="File" createDirs="true" fileName="C:\Test\Logs\${event-context:item=serviceName}.txt" archiveEvery="Day" archiveNumbering="Rolling" layout="${message}" />



and then just pass the service name to the logeventinfo

LogEventInfo lei = new LogEventInfo(LogLevel.Debug, logger.Name, "some message");
lei
.Properties["serviceName"] = serviceName;
logger
.Log(lei);

t0lik

unread,
Nov 7, 2012, 5:29:39 AM11/7/12
to nlog-...@googlegroups.com
I just can show you how it works in my code and you decide is is suitable for you or not:
1. Setting up file logging

            var config = new LoggingConfiguration();
           
var fileTarget = new FileTarget
                                 
{
                                     
Header = Layout.FromString(header),
                                     
CreateDirs = true,
                                     
Layout = Layout.FromString("${date:format=yyyy-MM-dd HH\\:mm\\:ss.fffffff} ${level} ${message}"),
                                     
FileName = String.Format("{0}\\{1}", logPath, logFileName),
                                     
Encoding = Encoding.GetEncoding(1251),
                                     
ArchiveFileName = String.Format("{0}\\{1}.{2}.log", logPath, logFileName, "{#####}"),
                                     
ArchiveNumbering = ArchiveNumberingMode.Sequence,
                                     
ArchiveEvery = FileArchivePeriod.None,
                                     
ArchiveAboveSize = 10 * 1024 * 1024,
                                     
AutoFlush = true,
                                     
ConcurrentWrites = false,
                                     
BufferSize = 10240
                                 
};
           
var asyncFileTarget = new AsyncTargetWrapper(fileTarget);
            config
.AddTarget("file", asyncFileTarget);
           
var fileLoggingRule = new LoggingRule("*", LogLevel.Info, asyncFileTarget);
            config
.LoggingRules.Add(fileLoggingRule);
           
LogManager.ThrowExceptions = true;
           
LogManager.Configuration = config;

2. Logging

            var logger = LogManager.GetLogger(typeof(T).FullName);
           
var resultMessage = string.Format(message, messageParams);
           
var logEvent = new LogEventInfo(level, typeof(T).FullName, resultMessage);
            logger
.Log(typeof(LoggingManager), logEvent);


Reply all
Reply to author
Forward
0 new messages