سؤال

I'm using c# + NLog and try to write to a MemoryTarget. I have the folloowing config:

        MemoryTarget = new MemoryTarget();

        CsvLayout csvLayout = new CsvLayout() { Delimiter = CsvColumnDelimiterMode.Comma, WithHeader = true };

        csvLayout.Columns.Add(new CsvColumn() { Name = "Name", Layout = "${event-context:item=Name}" });
        csvLayout.Columns.Add(new CsvColumn() { Name = "AuditTimeStamp", Layout = "${event-context:item=AuditTimeStamp}" });

        MemoryTarget.Layout = csvLayout;

(MemoryTarget is of course a NLog.MemoryTarget property)

So when I log stuff I can access the MemoryTarget via

MemoryTarget.Logs

What I don't get: also I have configured the WithHeader property in my CsvLayout I don't see a header in my MemoryTarget.Logs. I'd expect Name, AuditStamp in MemoryTarget.Logs[0] but it starts with the logged content right away.

Can anybody tell my why the header is not part of the MemoryTarget.Logs list?

هل كانت مفيدة؟

المحلول

Headers (and footers) only used by targets derevived from TargetWithLayoutHeaderAndFooter

Because the MemoryTarget is deriving from the TargetWithLayout and not from the TargetWithLayoutHeaderAndFooter setting the WithHeader = true does not have any effect on the the MemoryTarget. So you cannot get the header in MemoryTarget.Logs.

In order to the header outputted correctly you need to use one of targets which supports it:

  • Console
  • Colored Console
  • File
  • Mail
  • Debugger

Or you can directly access the generated header in your code with the help of LogEventInfo.CreateNullEvent() and calling:

string header = ((CsvLayout)MemoryTarget.Layout).Header
    .Render(LogEventInfo.CreateNullEvent());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top