问题
I'm new to NLog, and I've been playing with LogEventInfo objects, since I think I'll need them in my application. I created a LogEventInfo object with a plain text string, and then I called Logger.Debug(myEventInfoObject), using a FileAppender set up to write ${message}. Where I expected to see my text string, I saw instead the logger name, the message level, the message, and the sequence ID. I found that that expanded string is what I get when I call myEventInfoObject.ToString(). How can I control what appears when ${message} comes from a LogEventInfo object?
Here's my config file:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true">
<targets>
<target name="file" xsi:type="File" layout="${longdate} ${logger} ${message}" fileName="${basedir}/nlog_sample_file.txt" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
Here's my code generating the log:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
namespace NLogSample
{
class Program
{
static void Main(string[] args)
{
try
{
Logger logger = LogManager.GetCurrentClassLogger();
LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "This message from the LogInfoEvent will not be used.");
theEvent.Properties["Message"] = "This message from the LogInfoEvent Message property will be used.";
logger.Debug(theEvent);
logger.Debug("Does the sequence ID show up in this message?");
string formattedMessage = theEvent.FormattedMessage;
string eventString = theEvent.ToString();
}
catch (Exception ex)
{
int a = 1;
}
}
}
}
Finally, here is a sample of the message:
2014-10-08 10:14:13.5525 NLogSample.Program Log Event: Logger='' Level=Debug Message='This message from the LogInfoEvent will not be used.' SequenceID=2
I am working in Visual Studio 2012 on a Win7 Pro machine.
Thanks very much!
RobR
回答1:
I had to do this
class MyLogEventInfo : LogEventInfo {
public override string ToString() {
return base.FormattedMessage;
}
}
...
var theEvent = new MyLogEventInfo();
theEvent.Message = "This message from the LogInfoEvent will not be used.";
logger.Debug(theEvent);
回答2:
If you want to create LogEventInfo
objects and fill them with your own information, you should use one of the Logger.Log
method signatures, not the Logger.Info/Logger.Debug/etc
method signatures.
The behavior you are seeing right now is probably because the Logger.Debug
method is receiving an object (which happens to be a LogEventInfo
object) and is calling ToString
on it before logging it.
回答3:
@wageoghe is right. Here is how I use it:
// Create the logger
Logger logger = LogManager.GetCurrentClassLogger();
// Create the LogEventInfo object
LogEventInfo logEvent = new LogEventInfo();
// Now add the event characteristics
logEvent.Properties["EventCode"] = eventId;
logEvent.Level = level;
logEvent.Message = message;
logEvent.Exception = ex;
// Actually write the log entry.
logger.Log(logEvent);
No need to use the wrapper log objects, just define your LogLevel using the Level property in LogEventInfo()
and then pass that object to Log()
.
来源:https://stackoverflow.com/questions/26259494/nlog-how-do-i-control-the-format-of-a-message-from-a-logeventinfo-object