1

I am recently using Nlog in an ASP.Net project for a client with good success. I need to connect to an existing application that has its own event log management and provide them with a copy of my log information.

Is there any way to get the strings that I provided to Nlog using any of the log functions?

Example:

logger.Info("My info");
logger.Debug("My debug");
logger.Error("My error");
logger.Warn("My warn");
List<string> messages = logger.getStrings() <-- this is what I need

How I can get strings "My info", "My debug", "My error" and "My warn" as an array or List<>?

Julian
  • 33,915
  • 22
  • 119
  • 174
Miguel Febres
  • 2,153
  • 2
  • 21
  • 31
  • Are your logs written to a database? That would be the easiest way of getting log data from one application in another. You can also use a `MemoryAppender` and get its internal buffer. –  Jul 05 '17 at 18:33
  • @amy `MemoryAppender`? Sounds like log4net? – Julian Jul 06 '17 at 10:03
  • @Julian Oops, I was channeling my inner NLog. –  Jul 06 '17 at 12:34

1 Answers1

4

You could use the MemoryTarget for it:

nlog.config:

<nlog internalLogFile="c:\tempp\nlog-internal.log" internalLogLevel="Info">
    <targets>
        <target xsi:type="Memory" name="target1" layout="${message}" />
    </targets>
    <rules>
        <logger name="*" writeTo="target1" />
    </rules>
</nlog>

C#:

var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
IList<string> messages = target.Logs;

The messages are strings, rendered from the layout attribute in nlog.config

API docs for MemoryTarget

If you need to write them to another component, I would recommend to create a custom target

Julian
  • 33,915
  • 22
  • 119
  • 174