I would suggest writing LayoutRendererWrapper . LayoutRendererWrapper allows you to βwrapβ LayoutRenderer so that you can apply processing to the output. For stack trace encryption, you can configure NLog to add StackTrace to the output, but you can wrap the StackTrace layout renderer so you can apply your encryption.
You see examples of LayuoutRendererWrappers in the NLog Source Code Repository .
Actually, the general nature of LayoutRendererWrapper means that you can write an encryption shell and apply it to any LayoutRenderer. Thus, you can, for example, encrypt the stack trace and message, but leave the remaining fields as clear text.
Here is an example (untested) about how you can write LayoutRendererWrapper encryption:
namespace NLog.LayoutRenderers.Wrappers { using System.ComponentModel; using System.Globalization; using NLog.Config; [LayoutRenderer("Encrypt")] [AmbientProperty("Encrypt")] [ThreadAgnostic] public sealed class EncryptLayoutRendererWrapper : WrapperLayoutRendererBase { public EncryptLayoutRendererWrapper() { this.Culture = CultureInfo.InvariantCulture; this.Encrypt = true; } [DefaultValue(true)] public bool Encrypt { get; set; } public CultureInfo Culture { get; set; } protected override string Transform(string text) { return this.Encrypt ? Encrypt(text) : text; } protected string Encrypt(string text) {
I think it will be configured like this in the NLog.config file:
${longdate} | ${logger} | ${level} | ${encrypt:${stacktrace}} | ${message}
I am not sure how you will program it programmatically, since I usually do not use software configuration.
source share