Skip to content

Commit a3c0ed2

Browse files
committed
feat(NLog): use proper config pattern
1 parent 5a58bc6 commit a3c0ed2

File tree

6 files changed

+109
-55
lines changed

6 files changed

+109
-55
lines changed

Diff for: Ark.Tools.NLog.CloudConfigurationManager/NLogConfigurer.CloudConfigurationManager.cs

+15-12
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,30 @@ public static class NLogConfigurerCloudConfigurationManager
1010

1111
public static Configurer WithDefaultTargetsAndRulesFromCloudConfiguration(this Configurer @this, string logTableName, string mailFrom, string mailTo, bool async = true)
1212
{
13-
var smtp = new SmtpConnectionBuilder(CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpConnStringName))
13+
var smtp = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpConnStringName)
1414
?? new SmtpConnectionBuilder()
1515
{
1616
Server = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpServer),
1717
Port = int.Parse(CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpPort)),
1818
Username = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpUserName),
1919
Password = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpPassword),
2020
UseSsl = bool.Parse(CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SmtpUseSsl))
21-
};
21+
}.ConnectionString;
2222

23-
@this.WithArkDefaultTargetsAndRules(
24-
logTableName, CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SqlConnStringName),
25-
CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.MailNotificationAddresses) ?? mailTo, smtp.ConnectionString,
26-
mailFrom, async: async);
23+
var cfg = new Config
24+
{
25+
SQLConnectionString = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SqlConnStringName),
26+
SQLTableName = logTableName,
27+
SmtpConnectionString = smtp,
28+
MailTo = mailTo ?? CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.MailNotificationAddresses),
29+
MailFrom = mailFrom,
30+
SlackWebhook = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SlackWebHook),
31+
ApplicationInsightsInstrumentationKey = CloudConfigurationManager.GetSetting("APPINSIGHTS_INSTRUMENTATIONKEY")
32+
?? CloudConfigurationManager.GetSetting("ApplicationInsights:InstrumentationKey"),
33+
Async = async
34+
};
2735

28-
var cfgSlack = CloudConfigurationManager.GetSetting(NLogDefaultConfigKeys.SlackWebHook);
29-
if (!string.IsNullOrWhiteSpace(cfgSlack))
30-
@this.WithSlackDefaultTargetsAndRules(cfgSlack, async);
31-
32-
var iKey = CloudConfigurationManager.GetSetting("APPINSIGHTS_INSTRUMENTATIONKEY") ?? CloudConfigurationManager.GetSetting("ApplicationInsights:InstrumentationKey");
33-
@this.WithApplicationInsightsTargetsAndRules(iKey, async);
36+
@this.WithArkDefaultTargetsAndRules(cfg);
3437

3538
return @this;
3639
}

Diff for: Ark.Tools.NLog.Configuration/NLogConfigurer.Configuration.cs

+22-20
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@ public static class NLogConfigurerConfiguration
1717
{
1818
public static Configurer WithDefaultTargetsAndRulesFromConfiguration(this Configurer @this, IConfiguration cfg, bool async = true)
1919
{
20-
@this.WithArkDefaultTargetsAndRules(
21-
@this.AppName, cfg.GetConnectionString(NLogDefaultConfigKeys.SqlConnStringName),
22-
cfg[NLogDefaultConfigKeys.MailNotificationAddresses.Replace('.', ':')], cfg.GetConnectionString(NLogDefaultConfigKeys.SmtpConnStringName),
23-
async:async);
24-
25-
var cfgSlack = cfg[NLogDefaultConfigKeys.SlackWebHook.Replace('.', ':')];
26-
if (!string.IsNullOrWhiteSpace(cfgSlack))
27-
@this.WithSlackDefaultTargetsAndRules(cfgSlack, async);
20+
var config = new Config
21+
{
22+
SQLConnectionString = cfg.GetConnectionString(NLogDefaultConfigKeys.SqlConnStringName),
23+
SmtpConnectionString = cfg.GetConnectionString(NLogDefaultConfigKeys.SmtpConnStringName),
24+
MailTo = cfg[NLogDefaultConfigKeys.MailNotificationAddresses.Replace('.', ':')],
25+
ApplicationInsightsInstrumentationKey = cfg["APPINSIGHTS_INSTRUMENTATIONKEY"] ?? cfg["ApplicationInsights:InstrumentationKey"],
26+
SlackWebhook = cfg[NLogDefaultConfigKeys.SlackWebHook.Replace('.', ':')],
27+
Async = async
28+
};
2829

29-
var key = cfg["APPINSIGHTS_INSTRUMENTATIONKEY"] ?? cfg["ApplicationInsights:InstrumentationKey"];
30-
@this.WithApplicationInsightsTargetsAndRules(key, async);
30+
@this.WithArkDefaultTargetsAndRules(config);
3131

3232
return @this;
3333
}
3434

3535
public static Configurer WithDefaultTargetsAndRulesFromConfiguration(this Configurer @this, IConfiguration cfg, string logTableName, string mailFrom = null, bool async = true)
3636
{
37-
@this.WithArkDefaultTargetsAndRules(
38-
logTableName, cfg.GetConnectionString(NLogDefaultConfigKeys.SqlConnStringName),
39-
cfg[NLogDefaultConfigKeys.MailNotificationAddresses.Replace('.', ':')], cfg.GetConnectionString(NLogDefaultConfigKeys.SmtpConnStringName),
40-
mailFrom, async: async);
41-
42-
var cfgSlack = cfg[NLogDefaultConfigKeys.SlackWebHook.Replace('.', ':')];
43-
if (!string.IsNullOrWhiteSpace(cfgSlack))
44-
@this.WithSlackDefaultTargetsAndRules(cfgSlack, async);
37+
var config = new Config
38+
{
39+
SQLConnectionString = cfg.GetConnectionString(NLogDefaultConfigKeys.SqlConnStringName),
40+
SQLTableName = logTableName,
41+
SmtpConnectionString = cfg.GetConnectionString(NLogDefaultConfigKeys.SmtpConnStringName),
42+
MailTo = cfg[NLogDefaultConfigKeys.MailNotificationAddresses.Replace('.', ':')],
43+
MailFrom = mailFrom,
44+
ApplicationInsightsInstrumentationKey = cfg["APPINSIGHTS_INSTRUMENTATIONKEY"] ?? cfg["ApplicationInsights:InstrumentationKey"],
45+
SlackWebhook = cfg[NLogDefaultConfigKeys.SlackWebHook.Replace('.', ':')],
46+
Async = async
47+
};
4548

46-
var key = cfg["APPINSIGHTS_INSTRUMENTATIONKEY"] ?? cfg["ApplicationInsights:InstrumentationKey"];
47-
@this.WithApplicationInsightsTargetsAndRules(key, async);
49+
@this.WithArkDefaultTargetsAndRules(config);
4850

4951
return @this;
5052
}

Diff for: Ark.Tools.NLog.ConfigurationManager/NLogConfigurer.ConfigurationManager.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ public static class NLogConfigurerConfigurationManager
1212

1313
public static Configurer WithDefaultTargetsAndRulesFromAppSettings(this Configurer @this, string logTableName, string mailFrom, string mailTo, bool async = true)
1414
{
15-
var smtp = new SmtpConnectionBuilder(ConfigurationManager.ConnectionStrings[NLogDefaultConfigKeys.SmtpConnStringName].ConnectionString)
15+
var smtp = ConfigurationManager.ConnectionStrings[NLogDefaultConfigKeys.SmtpConnStringName].ConnectionString
1616
?? new SmtpConnectionBuilder()
1717
{
1818
Server = ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SmtpServer],
1919
Port = int.Parse(ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SmtpPort]),
2020
Username = ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SmtpUserName],
2121
Password = ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SmtpPassword],
2222
UseSsl = bool.Parse(ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SmtpUseSsl])
23-
};
24-
25-
@this.WithArkDefaultTargetsAndRules(
26-
logTableName, ConfigurationManager.ConnectionStrings[NLogDefaultConfigKeys.SqlConnStringName].ConnectionString,
27-
ConfigurationManager.AppSettings[NLogDefaultConfigKeys.MailNotificationAddresses] ?? mailTo, smtp.ConnectionString,
28-
mailFrom, async: async);
29-
30-
var cfgSlack = ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SlackWebHook];
31-
if (!string.IsNullOrWhiteSpace(cfgSlack))
32-
@this.WithSlackDefaultTargetsAndRules(cfgSlack, async);
33-
34-
var iKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] ?? ConfigurationManager.AppSettings["ApplicationInsights:InstrumentationKey"];
35-
@this.WithApplicationInsightsTargetsAndRules(iKey, async);
23+
}.ConnectionString;
24+
25+
var config = new Config
26+
{
27+
SQLConnectionString = ConfigurationManager.ConnectionStrings[NLogDefaultConfigKeys.SqlConnStringName].ConnectionString,
28+
SQLTableName = logTableName,
29+
SmtpConnectionString = smtp,
30+
MailTo = mailTo ?? ConfigurationManager.AppSettings[NLogDefaultConfigKeys.MailNotificationAddresses],
31+
MailFrom = mailFrom,
32+
SlackWebhook = ConfigurationManager.AppSettings[NLogDefaultConfigKeys.SlackWebHook],
33+
ApplicationInsightsInstrumentationKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"]
34+
?? ConfigurationManager.AppSettings["ApplicationInsights:InstrumentationKey"],
35+
Async = async
36+
};
37+
38+
@this.WithArkDefaultTargetsAndRules(config);
3639

3740
return @this;
3841
}

Diff for: Ark.Tools.NLog/NlogConfigurer.cs

+30-8
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,54 @@ public static Configurer WithApplicationInsightsDefaultRules(this Configurer @th
5555
;
5656
}
5757

58-
public static Configurer WithArkDefaultTargetsAndRules(this Configurer @this, string logTableName, string connectionString, string mailTo, string smtpConnectionString, string mailFrom = null, bool? consoleEnabled = null, bool async = true)
58+
public record Config(
59+
string SQLConnectionString = null,
60+
string SQLTableName = null,
61+
string SmtpConnectionString = null,
62+
string MailTo = null,
63+
string MailFrom = null,
64+
string SlackWebhook = null,
65+
string ApplicationInsightsInstrumentationKey = null,
66+
bool? EnableConsole = null,
67+
bool Async = true);
68+
69+
70+
public static Configurer WithArkDefaultTargetsAndRules(this Configurer @this, Config config)
5971
{
60-
consoleEnabled ??= !_isProduction();
72+
var consoleEnabled = config.EnableConsole ?? !_isProduction();
6173

6274
if (consoleEnabled == true)
6375
{
6476
@this
65-
.WithConsoleTarget(async)
77+
.WithConsoleTarget(config.Async)
6678
.WithConsoleRule("*", _isProduction() ? LogLevel.Info : LogLevel.Trace);
6779
}
6880

69-
if (!string.IsNullOrWhiteSpace(connectionString))
81+
if (!string.IsNullOrWhiteSpace(config.SQLConnectionString))
7082
{
7183
@this
72-
.WithDatabaseTarget(logTableName, connectionString, async)
84+
.WithDatabaseTarget(config.SQLTableName ?? @this.AppName, config.SQLConnectionString, config.Async)
7385
.WithDatabaseRule("*", LogLevel.Info);
7486
}
7587

76-
if (!string.IsNullOrWhiteSpace(smtpConnectionString))
88+
if (!string.IsNullOrWhiteSpace(config.SmtpConnectionString))
7789
{
7890
@this
79-
.WithMailTarget(mailFrom, mailTo, smtpConnectionString, async: false)
91+
.WithMailTarget(config.MailFrom, config.MailTo, config.SmtpConnectionString, async: false)
8092
.WithMailRule("*", LogLevel.Fatal)
8193
;
8294
}
8395

96+
if (!string.IsNullOrWhiteSpace(config.SlackWebhook))
97+
{
98+
@this.WithSlackDefaultTargetsAndRules(config.SlackWebhook, config.Async);
99+
}
100+
101+
if (!string.IsNullOrWhiteSpace(config.ApplicationInsightsInstrumentationKey))
102+
{
103+
@this.WithApplicationInsightsTargetsAndRules(config.ApplicationInsightsInstrumentationKey, config.Async);
104+
}
105+
84106
if (Debugger.IsAttached)
85107
{
86108
@this
@@ -129,7 +151,7 @@ private static string _getEnvironment()
129151
{
130152
return Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")
131153
?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
132-
?? "";
154+
?? "Production";
133155
}
134156

135157
[Obsolete("Use .WithDefaultTargetsAndRulesFromConfiguration() from Ark.Tools.NLog.Configuration. Beware to use connectionString:NLog.Smtp", true)]

Diff for: Directory.Build.props

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
</PropertyGroup>
4646

4747
<ItemGroup>
48-
<None Include="..\ark-dark.png" Pack="true" PackagePath="\"/>
48+
<None Include="..\ark-dark.png" Pack="true" PackagePath="\"/>
49+
<Compile Include="..\ExternalInitBecauseMSWtf.cs" />
4950
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
5051
</ItemGroup>
5152

@@ -79,6 +80,7 @@
7980
<Exec Command="dotnet nuget push -s https://api.nuget.org/v3/index.json $(MSBuildProjectDirectory)\$(OutputPath)$(PackageId).$(PackageVersion).nupkg" />
8081
</Target>
8182

83+
8284

8385

8486
</Project>

Diff for: ExternalInitBecauseMSWtf.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
6+
7+
using System.ComponentModel;
8+
9+
// ReSharper disable once CheckNamespace
10+
namespace System.Runtime.CompilerServices
11+
{
12+
/// <summary>
13+
/// Reserved to be used by the compiler for tracking metadata.
14+
/// This class should not be used by developers in source code.
15+
/// </summary>
16+
[EditorBrowsable(EditorBrowsableState.Never)]
17+
internal static class IsExternalInit
18+
{
19+
}
20+
}
21+
22+
#endif

0 commit comments

Comments
 (0)