Skip to content

Commit

Permalink
Merge pull request #244 from ARKlab/bugfix/NLog_JsonSerializer_Error
Browse files Browse the repository at this point in the history
fix(NLog): revert to use NLog internal JsonSerializer
  • Loading branch information
AndreaCuneo authored Oct 3, 2024
2 parents 6f3c89d + 08ff569 commit 7fbe457
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Ark.Tools.NLog.Configuration/NLogConfigurer.Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static IHostBuilder ConfigureNLog(this IHostBuilder builder, string? appN
return builder.ConfigureLogging((ctx, logging) =>
{
var c = NLogConfigurer.For(appName)
.WithDefaultTargetsAndRulesFromConfiguration(ctx.Configuration, appName, mailFrom, async: !ctx.HostingEnvironment.IsEnvironment("SpecFlow"))
.WithDefaultTargetsAndRulesFromConfiguration(ctx.Configuration, appName, mailFrom,
async: !ctx.HostingEnvironment.EnvironmentName.Contains("SpecFlow", StringComparison.OrdinalIgnoreCase))
;
configure ?.Invoke(c);
Expand Down
28 changes: 17 additions & 11 deletions Ark.Tools.NLog/NlogConfigurer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public static class NLogConfigurer
public const string MailTarget = "Ark.Mail";
public const string MailFromDefault = "[email protected]";

public const string TextLineLayout = @"${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${pad:padding=-20:inner=${logger:shortName=true}} ${message}${onexception:${newline}${exception:format=ToString}}";

static NLogConfigurer()
{
LogManager.Setup().SetupExtensions(b => b
.RegisterAssembly(typeof(Configurer).Assembly)
.RegisterAssembly(typeof(ActivityTraceLayoutRenderer).Assembly));
LogManager.LogFactory.ServiceRepository.RegisterService(typeof(IJsonConverter), new STJSerializer());
LogManager.Setup()
.SetupExtensions(b => b
.RegisterAssembly(typeof(Configurer).Assembly)
.RegisterAssembly(typeof(ActivityTraceLayoutRenderer).Assembly))
;

// This has been added support NLog loggers output to Console during application initialization,
// before Configuration is Read and Host is Built.
Expand Down Expand Up @@ -222,7 +225,7 @@ public Configurer WithDebuggerTarget()
{
_config.AddTarget("Debugger", new DebuggerTarget("Debugger")
{
Layout= @"${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${pad:padding=-20:inner=${logger:shortName=true}} ${message}${onexception:${newline}${exception:format=ToString}}"
Layout= TextLineLayout
});
return this;
}
Expand Down Expand Up @@ -266,8 +269,11 @@ public Configurer WithApplicationInsightsTarget(string instrumentationKey, bool

public Configurer WithConsoleTarget(bool async = true)
{
var consoleTarget = new ColoredConsoleTarget();
consoleTarget.Layout = @"${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${pad:padding=-20:inner=${logger:shortName=true}} ${message}${onexception:${newline}${exception:format=ToString}}";
var consoleTarget = new ConsoleTarget();
consoleTarget.WriteBuffer = async;
consoleTarget.AutoFlush = !async;
consoleTarget.Layout = TextLineLayout;

_config.AddTarget(ConsoleTarget, async ? _wrapWithAsyncTargetWrapper(consoleTarget) as Target : consoleTarget);
return this;
}
Expand All @@ -276,7 +282,7 @@ public Configurer WithFileTarget(bool async = true)
{
var fileTarget = new FileTarget();

fileTarget.Layout = @"${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${pad:padding=-20:inner=${logger:shortName=true}} ${message}${onexception:${newline}${exception:format=ToString}}";
fileTarget.Layout = TextLineLayout;
fileTarget.FileName = @"${basedir}\Logs\Trace.log";
fileTarget.KeepFileOpen = true;
fileTarget.ConcurrentWrites = false;
Expand Down Expand Up @@ -374,7 +380,7 @@ private MailTarget _getBasicMailTarget()
var target = new MailTarget();
target.AddNewLines = true;
target.Encoding = Encoding.UTF8;
target.Layout = @"${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${pad:padding=-20:inner=${logger:shortName=true}} ${message}${onexception:${newline}${exception:format=ToString}}";
target.Layout = TextLineLayout;
target.Html = true;
target.ReplaceNewlineWithBrTagInHtml = true;
target.Subject = "Errors from ${scopeproperty:item=AppName:whenempty=${gdc:item=AppName}}@${ark.hostname}";
Expand Down Expand Up @@ -680,13 +686,13 @@ public bool SerializeObject(object value, StringBuilder builder)
try
{
builder.Append(JsonSerializer.Serialize(value, ArkSerializerOptions.JsonOptions));
return true;
}
catch (Exception e)
{
InternalLogger.Error(e, "Error when custom JSON serialization");
InternalLogger.Error(e, "Error when serializing type '{type}' '{string}' as json.", value?.GetType(), value?.ToString());
return false;
}
return true;
}
}
}
Expand Down

0 comments on commit 7fbe457

Please sign in to comment.