NLogViewer icon indicating copy to clipboard operation
NLogViewer copied to clipboard

Implementing in code (not nlog.confg)

Open MHLoppy opened this issue 3 years ago • 2 comments

I've been wrestling with implementing this for the past little while (trying to adapt the instructions for nlog.config to their in-code equivalents) but can't seem to get it right.

In particular, I'm struggling with the equivalents for:

  • adding the assembly/extension (would adapting this be right? https://stackoverflow.com/questions/30340414/nlog-extensions-add-assembly-programmatically)
  • setting async to true for targets
  • adding a target of type CacheTarget

Do you have any pointers? I've been looking through the NLog documentation and searching online for a bit but haven't found much help except for that link regarding the assembly loading.

MHLoppy avatar Oct 22 '21 14:10 MHLoppy

Hello,

Have you found a solution in the meantime or is the issue still relevant?

djonasdev avatar Jun 27 '22 09:06 djonasdev

The project I was implementing this in isn't something I've worked on recently (so I don't remember exact details), but I believe I was able to figure out the first and third points, and simply gave up on implementing the second because I couldn't find a solution (and it wasn't critically important to implement for me). I imagine that it's something that could still be done though.

For 1), adapting the information in that link did seem to be the solution. I implemented:

var assembly = Assembly.Load("NLogViewer");
NLog.Config.ConfigurationItemFactory.Default.RegisterItemsFromAssembly(assembly);

For 3), I implemented:

DJ.Targets.CacheTarget CacheTarget = new DJ.Targets.CacheTarget();//the important part of this is this line
config.AddRule(LogLevel.Debug, LogLevel.Fatal, CacheTarget);//this line is just additional context, rather than the solution itself

and I think that did what I wanted.

MHLoppy avatar Jun 27 '22 10:06 MHLoppy