I have the following class :
[DataContract]
public class CallInformation
{
[DataMember]
public string Address { get; set; }
[DataMember]
public Boolean IsEmpty { get; set; }
[DataMember]
public Boolean IsFaulted { get; set; }
[DataMember]
public string Action { get; set; }
[DataMember]
public CallOrder CallDirection { get; set; }
[DataMember]
public DateTime EventTime { get; set; }
[DataMember]
public TimeSpan Duration { get; set; }
[DataMember]
public Boolean IsCallback { get; set; }
[DataMember]
public string LogSource { get; set; } = "Unknown";
[DataMember]
public string Soap { get; set; }
public string EventTimeDisplay
{
get { return EventTime.ToString("HH:mm:ss.fffffff"); }
set { }
}
}
This is filled with data about communication in a client server application and den sent to NLog :
public void LogCommunication(CallInformation callInfo)
{
var logEvent = new LogEventInfo(LogLevel.Trace, "CommunicationLogger", "CommunicationLogger is logging");
logEvent.Properties["CallInformation"] = callInfo;
_comLogger.Log(logEvent);
}
If it correct to put it in the properties or should it be put in the Parameters?
I need NLog to log it to file in a way that it later can be picked up and searched by Filebeat, ElasticSearch and Kibana. I have tried this NLog config :
<logger name="CommunicationLogger" minlevel="Trace" writeto="f"></logger>
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${event-properties:item=CallInformation} ${message}" />
But all it prints is CommunicationLogger is logging? I suspect that I need it to serialize the whole object in some way?
Regards
Edit 1 I have tried to change the layout like this :
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${event-properties:item=CallInformation:jsonEncode=true} ${message}" />
This does still not work, but if I change this code it workes :
logEvent.Properties["CallInformation"] = "test"; //callInfo;
test is written to file as it should so there is probably a problem to pars the simple class CallInformation to file.
I have checked the internal-log for NLog but I can´t finde any errors there.
Edit 2
I tried to change the code like this :
//var logEvent = new LogEventInfo(LogLevel.Trace, "CommunicationLogger", "ComLog writing.");
//logEvent.Properties["CallInformation"] = callInfo;
_comLogger.Log(LogLevel.Trace, "Test to log {@CallInformation}", callInfo);
nlog.config
<target xsi:type="File"
name="communicationFileLog"
fileName="${basedir}/logs/${shortdate}.log"
maxArchiveDays="5"
maxArchiveFiles="10"
layout="${event-properties:item=CallInformation:format@} ${message}" />
The result in the log file is this :
Test to log {@CallInformation}
Test to log {@CallInformation}
Test to log {@CallInformation}
Test to log {@CallInformation}
...