Skip to content

Commit

Permalink
[new] 支持nlog配置日志格式而不是写死在代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Feb 24, 2024
1 parent 7768d28 commit fccae1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/Luban/Luban.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
</ItemGroup>

<ItemGroup>
<None Update="nlog.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\common\flatbuffers\enum.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
22 changes: 4 additions & 18 deletions src/Luban/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private class CommandOptions
[Option('x', "xargs", Required = false, HelpText = "args like -x a=1 -x b=2")]
public IEnumerable<string> Xargs { get; set; }

[Option('l', "logConfig", Required = false, Default = "nlog.xml", HelpText = "nlog config file")]
public string LogConfig { get; set; }

[Option('v', "verbose", Required = false, HelpText = "verbose")]
public bool Verbose { get; set; }
}
Expand Down Expand Up @@ -222,7 +225,7 @@ private static void SetupApp(CommandOptions opts)
ThreadPool.SetMinThreads(Math.Max(4, processorCount), 0);
ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 2), 2);

InitSimpleNLogConfigure(opts.Verbose ? LogLevel.Trace : LogLevel.Info);
NLog.LogManager.Setup().LoadConfigurationFromFile(opts.LogConfig);
s_logger = LogManager.GetCurrentClassLogger();
PrintCopyRight();
}
Expand All @@ -235,21 +238,4 @@ private static void PrintCopyRight()
s_logger.Info("");
s_logger.Info(" =============================================================================================");
}

private static void InitSimpleNLogConfigure(NLog.LogLevel minConsoleLogLevel)
{
var logConfig = new NLog.Config.LoggingConfiguration();
NLog.Layouts.Layout layout;
if (minConsoleLogLevel <= NLog.LogLevel.Debug)
{
layout = NLog.Layouts.Layout.FromString("${longdate}|${level:uppercase=true}|${callsite}:${callsite-linenumber}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}");
}
else
{
layout = NLog.Layouts.Layout.FromString("${longdate}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}");
}
logConfig.AddTarget("console", new NLog.Targets.ColoredConsoleTarget() { Layout = layout });
logConfig.AddRule(minConsoleLogLevel, NLog.LogLevel.Fatal, "console");
NLog.LogManager.Configuration = logConfig;
}
}
25 changes: 25 additions & 0 deletions src/Luban/nlog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
<default-wrapper xsi:type="AsyncWrapper"
name="default-wrapper"
queueLimit="1000000"
timeToSleepBetweenBatches="1"
batchSize="1000"
overflowAction="Block"
/>

<target name="logconsole" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false"
layout="${date}|${level:uppercase=true}|${message}${onexception:${newline}${exception:format=tostring}${exception:format=StackTrace}}" >
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />
</target>
</targets>

<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
</rules>
</nlog>

0 comments on commit fccae1b

Please sign in to comment.