Skip to content

Commit

Permalink
Update readme content.
Browse files Browse the repository at this point in the history
  • Loading branch information
JFTCoCo committed Jan 10, 2019
1 parent eae8cd4 commit 9f0112e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
104 changes: 52 additions & 52 deletions NLog.Appsettings.Standard/AppSettingsLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
Expand All @@ -12,70 +12,70 @@ public class AppSettingsLayoutRenderer : LayoutRenderer
{
private static IConfigurationRoot _configurationRoot;

internal IConfigurationRoot DefaultAppSettings
{
get => _configurationRoot;
set => _configurationRoot = value;
}
internal IConfigurationRoot DefaultAppSettings
{
get => _configurationRoot;
set => _configurationRoot = value;
}

/// <summary>
/// <summary>
/// Global configuration. Used if it has set
/// </summary>
public static IConfiguration AppSettings { private get; set; }

///<summary>
/// The AppSetting name.
///</summary>
[RequiredParameter]
[DefaultParameter]
public string Name { get; set; }
/// The AppSetting name.
///</summary>
[RequiredParameter]
[DefaultParameter]
public string Name { get; set; }

///<summary>
/// The default value to render if the AppSetting value is null.
///</summary>
public string Default { get; set; }

///<summary>
/// The default value to render if the AppSetting value is null.
///</summary>
public string Default { get; set; }
public AppSettingsLayoutRenderer() {

public AppSettingsLayoutRenderer() {

if(AppSettings == null && DefaultAppSettings == null)
{
DefaultAppSettings = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true).Build();
}
}
if(AppSettings == null && DefaultAppSettings == null)
{
DefaultAppSettings = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true).Build();
}
}

/// <summary>
/// Renders the specified application setting or default value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (Name == null) return;
/// <summary>
/// Renders the specified application setting or default value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (Name == null) return;

string value = AppSettingValue;
if (value == null && Default != null)
value = Default;
string value = AppSettingValue;
if (value == null && Default != null)
value = Default;

if (string.IsNullOrEmpty(value) == false)
builder.Append(value);
}
if (string.IsNullOrEmpty(value) == false)
builder.Append(value);
}

private bool _cachedAppSettingValue = false;
private string _appSettingValue = null;
private string AppSettingValue
{
get
{
Name = Name.Replace('.',':');
if (_cachedAppSettingValue == false)
private bool _cachedAppSettingValue = false;
private string _appSettingValue = null;
private string AppSettingValue
{
_appSettingValue = AppSettings == null ? DefaultAppSettings[Name] : AppSettings[Name];
_cachedAppSettingValue = true;
get
{
Name = Name.Replace('.',':');
if (_cachedAppSettingValue == false)
{
_appSettingValue = AppSettings == null ? DefaultAppSettings[Name] : AppSettings[Name];
_cachedAppSettingValue = true;
}
return _appSettingValue;
}
}
return _appSettingValue;
}
}
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ Target appsettings.json
* **${appsettings:name=Options.StorageConnectionString}** - Get "abcdefg123456789" in this case.
* **${appsettings:name=Options.StorageConnectionString2:default=DefaultString}** - Get "DefaultString" in this case.

### Set Explicit Configuration ###
In some cases, the library may not work correctly (e.g., always access incorrect appsettings.json). You can set the configuration directly by use the global property ``AppSettings`` before you start logging work as follows

```C#
using NLog.Appsettings.Standard;

..........

AppSettingsLayoutRenderer.AppSettings = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").AddJsonFile($"appsettings.Development.json", optional: true)
.Build();
..........
```

## Test App ##
NLog.Appsettings.Standard.Test is a console program that is preconfigured to use the ``appsettings`` layout renderer. It is a good sample that you can follow.

Expand Down

0 comments on commit 9f0112e

Please sign in to comment.