Skip to content

Commit

Permalink
Version 5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Nov 27, 2022
1 parent 2b8583f commit 3701d3b
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 68 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ See also [releases](https://github.com/NLog/NLog.Web/releases) and [milestones](

Date format: (year/month/day)

### Version 5.2.0 (2022/11/27)

- **NLog.Web.AspNetCore**
- [#885](https://github.com/NLog/NLog.Web/pull/885) Changed LoadConfigurationFromAppSettings to prioritize current directory (#885) (@snakefoot)
- [#887](https://github.com/NLog/NLog.Web/pull/887) Changed AspNetAppBasePathLayoutRenderer to prioritize current directory (#887) (@snakefoot)
- [#890](https://github.com/NLog/NLog.Web/pull/890) Add ObjectPath to AspNetItemValueLayoutRenderer as better alternative to EvaluateAsNestedProperties (#890) (@bakgerman)
- [#889](https://github.com/NLog/NLog.Web/pull/889) Use CallerArgumentExpressionAttribute in helper method for throwing ArgumentNullException (#889) (@bakgerman)
- [#894](https://github.com/NLog/NLog.Web/pull/894) Use AspNetSessionValueLayoutRenderer in helper method for throwing ArgumentNullException (#894) (@snakefoot)
- [#891](https://github.com/NLog/NLog.Web/pull/891) Skip allocating RouteData when only need to lookup single value (#891) (@snakefoot)
- [#878](https://github.com/NLog/NLog.Web/pull/878) + [#879](https://github.com/NLog/NLog.Web/pull/879) Use RegisterConfigSettings instead of ConfigSettingLayoutRenderer (#878 + #879) (@snakefoot)
- [#894](https://github.com/NLog/NLog.Web/pull/894) Updated dependency NLog.Extensions.Logging v5.2.0 (#894) (@snakefoot)

- **NLog.Web**
- [#890](https://github.com/NLog/NLog.Web/pull/890) Add ObjectPath to AspNetItemValueLayoutRenderer as better alternative to EvaluateAsNestedProperties (#890) (@bakgerman)
- [#894](https://github.com/NLog/NLog.Web/pull/894) Use AspNetSessionValueLayoutRenderer in helper method for throwing ArgumentNullException (#894) (@snakefoot)
- [#889](https://github.com/NLog/NLog.Web/pull/889) Use CallerArgumentExpressionAttribute in helper method for throwing ArgumentNullException (#889) (@bakgerman)
- [#894](https://github.com/NLog/NLog.Web/pull/894) Updated dependency NLog v5.1.0 (#894) (@snakefoot)

### Version 5.1.5 (2022/10/26)

- **NLog.Web.AspNetCore**
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 5.1.0.{build}
version: 5.2.0.{build}
clone_folder: c:\projects\nlogweb
configuration: Release
image: Visual Studio 2022
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# creates NuGet package at \artifacts
dotnet --version

$versionPrefix = "5.1.5" # Also update version for minor versions in appveyor.yml
$versionPrefix = "5.2.0" # Also update version for minor versions in appveyor.yml
$versionSuffix = ""
$versionFile = $versionPrefix + "." + ${env:APPVEYOR_BUILD_NUMBER}
if ($env:APPVEYOR_PULL_REQUEST_NUMBER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.1.2" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="NLog" version="5.0.5" targetFramework="net461" />
<package id="NLog" version="5.1.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
<package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>
12 changes: 9 additions & 3 deletions src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ Supported platforms:
<PackageId>NLog.Web.AspNetCore</PackageId>
<PackageTags>logging;log;NLog;web;aspnet;aspnetcore;MVC;Microsoft.Extensions.Logging;httpcontext</PackageTags>
<PackageReleaseNotes>
- Introduced Introduced NET6 since LTS (@snakefoot)
- Updated NLog.Extensions.Logging to v5.1.0 (@snakefoot)
- Changed LoadConfigurationFromAppSettings to prioritize current directory (#885) (@snakefoot)
- Changed AspNetAppBasePathLayoutRenderer to prioritize current directory (#887) (@snakefoot)
- Add ObjectPath to AspNetItemValueLayoutRenderer as better alternative to EvaluateAsNestedProperties (#890) (@bakgerman)
- Add ObjectPath to AspNetSessionValueLayoutRenderer as better alternative to EvaluateAsNestedProperties (#894) (@snakefoot)
- Use CallerArgumentExpressionAttribute in helper method for throwing ArgumentNullException (#889) (@bakgerman)
- Skip allocating RouteData when only need to lookup single value (#891) (@snakefoot)
- Use RegisterConfigSettings instead of ConfigSettingLayoutRenderer (#878 + #879) (@snakefoot)
- Updated dependency NLog.Extensions.Logging v5.2.0 (#894) (@snakefoot)

List of major changes in NLog 5.0: https://nlog-project.org/2022/05/16/nlog-5-0-finally-ready.html

Expand Down Expand Up @@ -75,7 +81,7 @@ List of available Layout Renderers: https://nlog-project.org/config/?tab=layout-
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="5.1.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461' ">
<!-- Fixed to 2.1.0 as 2.1 is Long Term Supported (LTS) and only supported version for .NET Framework 4.6.1 -->
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ List of available Layout Renderers: https://nlog-project.org/config/?tab=layout-
<DownloadFile SourceUrl="https://nlog-project.org/N.png" DestinationFolder="$(MSBuildThisFileDirectory)" />
</Target>
<ItemGroup>
<PackageReference Include="NLog" Version="5.0.5" />
<PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
Expand Down
48 changes: 4 additions & 44 deletions src/Shared/Internal/PropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NLog.Web.Internal
{
[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
internal static class PropertyReader
{
/// <summary>
Expand All @@ -14,6 +15,7 @@ internal static class PropertyReader
/// <param name="getVal">function to get a value with this key</param>
/// <param name="evaluateAsNestedProperties">evaluate <paramref name="key" /> as a nested property path. E.g. A.B is property B inside A.</param>
/// <returns>value</returns>
[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
public static object GetValue<T>(string key, T container, Func<T, string, object> getVal, bool evaluateAsNestedProperties)
{
if (string.IsNullOrEmpty(key))
Expand All @@ -24,30 +26,7 @@ public static object GetValue<T>(string key, T container, Func<T, string, object
return evaluateAsNestedProperties ? GetValueAsNestedProperties(key, container, getVal) : getVal(container, key);
}

/// <summary>
/// Get value of a property
/// </summary>
/// <param name="key">key</param>
/// <param name="container">Container to perform value lookup using key</param>
/// <param name="getVal">function to get a value with this key</param>
/// <param name="objectPath">evaluate the string as a dot notated path to a aproperty in the object, returned by lookup by the key parameter</param>
/// <returns>value</returns>
public static object GetValue<T>(string key, T container, Func<T, string, object> getVal, string objectPath)
{
if (string.IsNullOrEmpty(key))
{
return null;
}

var value = getVal(container, key);
if (value == null)
{
return null;
}

return GetValueAsNestedProperties(value, objectPath);
}

[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
private static object GetValueAsNestedProperties<T>(string key, T container, Func<T, string, object> getVal)
{
var path = key.Contains('.') ? key.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries) : null;
Expand All @@ -69,26 +48,7 @@ private static object GetValueAsNestedProperties<T>(string key, T container, Fun
return value;
}

private static object GetValueAsNestedProperties(object value, string objectPath)
{
var path = objectPath.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

if (value != null && path?.Length > 0)
{
for (int i = 0; i < path.Length; ++i)
{
var propertyInfo = GetPropertyInfo(value, path[i]);
value = propertyInfo?.GetValue(value, null);
if (value == null)
{
break;
}
}
}

return value;
}

[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
private static PropertyInfo GetPropertyInfo(object value, string propertyName)
{
#if !ASP_NET_CORE
Expand Down
31 changes: 23 additions & 8 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-item")]
public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
{
private readonly NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper _objectPathRenderer = new NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper();

/// <summary>
/// Gets or sets the item variable name.
/// </summary>
Expand All @@ -53,7 +55,7 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
/// This will emit the First Name property of the object in HttpContext.Items woith the key of 'person' in the collection
/// </summary>
/// <docgen category='Layout Options' order='20' />
public string ObjectPath { get; set; }
public string ObjectPath { get => _objectPathRenderer.ObjectPath; set => _objectPathRenderer.ObjectPath = value; }

/// <summary>
/// Gets or sets the item variable name.
Expand All @@ -67,6 +69,7 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
/// invoked since ObjectPath is set
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
public bool EvaluateAsNestedProperties { get; set; }

/// <summary>
Expand All @@ -84,24 +87,36 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var context = HttpContextAccessor.HttpContext;
var item = Item;
if (string.IsNullOrEmpty(item))
{
return;
}

if (Item == null)
var context = HttpContextAccessor.HttpContext;
if (context is null)
{
return;
}

object value = null;

// Function using the Item string as the object path
if (ObjectPath == null)
if (ObjectPath is null)
{
value = PropertyReader.GetValue(Item, context?.Items, (items, key) => LookupItemValue(items, key), EvaluateAsNestedProperties);
#pragma warning disable CS0618 // Type or member is obsolete
value = PropertyReader.GetValue(item, context?.Items, (items, key) => LookupItemValue(items, key), EvaluateAsNestedProperties);
#pragma warning restore CS0618 // Type or member is obsolete
if (value is null)
return;
}
// Function using the ObjectPath as the object path, hard code evaluateNestedProperties argument to true
else
{
value = PropertyReader.GetValue(Item, context?.Items, (items, key) => LookupItemValue(items, key), ObjectPath);
value = LookupItemValue(context?.Items, item);
if (value is null)
return;

if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;
}

var formatProvider = GetFormatProvider(logEvent, Culture);
Expand Down
36 changes: 30 additions & 6 deletions src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-session")]
public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase
{
private readonly NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper _objectPathRenderer = new NLog.LayoutRenderers.Wrappers.ObjectPathRendererWrapper();

/// <summary>
/// Gets or sets the session item name.
/// </summary>
Expand All @@ -56,10 +58,16 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase
/// <docgen category='Rendering Options' order='10' />
public string Variable { get => Item; set => Item = value; }

/// <summary>
/// Gets or sets the object-property-navigation-path for lookup of nested property
/// </summary>
public string ObjectPath { get => _objectPathRenderer.ObjectPath; set => _objectPathRenderer.ObjectPath = value; }

/// <summary>
/// Gets or sets whether variables with a dot are evaluated as properties or not
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[Obsolete("Instead use ObjectPath. Marked obsolete with NLog.Web 5.2")]
public bool EvaluateAsNestedProperties { get; set; }

/// <summary>
Expand Down Expand Up @@ -99,13 +107,13 @@ public SessionValueType ValueType
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var item = Item;
if (item == null)
if (string.IsNullOrEmpty(item))
{
return;
}

var context = HttpContextAccessor.HttpContext;
if (context == null)
if (context is null)
{
return;
}
Expand All @@ -131,12 +139,28 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

var value = PropertyReader.GetValue(item, contextSession, _sessionValueLookup, EvaluateAsNestedProperties);
if (value != null)
object value = null;

if (ObjectPath is null)
{
var formatProvider = GetFormatProvider(logEvent, Culture);
builder.AppendFormattedValue(value, Format, formatProvider, ValueFormatter);
#pragma warning disable CS0618 // Type or member is obsolete
value = PropertyReader.GetValue(item, contextSession, _sessionValueLookup, EvaluateAsNestedProperties);
#pragma warning restore CS0618 // Type or member is obsolete
if (value is null)
return;
}
else
{
value = _sessionValueLookup(contextSession, item);
if (value is null)
return;

if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;
}

var formatProvider = GetFormatProvider(logEvent, Culture);
builder.AppendFormattedValue(value, Format, formatProvider, ValueFormatter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public void NestedPropertyRendersValueItem(string itemKey, string variable, obje
#endif
var culture = CultureInfo.CurrentUICulture;
renderer.Variable = variable;
#pragma warning disable CS0618 // Type or member is obsolete
renderer.EvaluateAsNestedProperties = true;
#pragma warning restore CS0618 // Type or member is obsolete
renderer.Culture = culture;

// Act
Expand All @@ -138,7 +140,9 @@ public void NestedPropertyRendersValueObjectPath(string itemKey, string variable
#endif
var culture = CultureInfo.CurrentUICulture;
renderer.Variable = variable;
#pragma warning disable CS0618 // Type or member is obsolete
renderer.EvaluateAsNestedProperties = true;
#pragma warning restore CS0618 // Type or member is obsolete
renderer.Culture = culture;

// Act
Expand Down Expand Up @@ -209,7 +213,9 @@ public void NestedItemRendersProperly()
#endif
renderer.Item = "person.Name.First";

#pragma warning disable CS0618 // Type or member is obsolete
renderer.EvaluateAsNestedProperties = true;
#pragma warning restore CS0618 // Type or member is obsolete

// Act
string result = renderer.Render(new LogEventInfo());
Expand Down Expand Up @@ -247,7 +253,9 @@ public void NestedObjectPathRendersProperly()
renderer.Item = "person";
renderer.ObjectPath = "Name.Last";

#pragma warning disable CS0618 // Type or member is obsolete
renderer.EvaluateAsNestedProperties = false;
#pragma warning restore CS0618 // Type or member is obsolete

// Act
string result = renderer.Render(new LogEventInfo());
Expand Down Expand Up @@ -284,8 +292,6 @@ public void NestedObjectPathRendersProperlyII()
renderer.Item = "person";
renderer.ObjectPath = "Name.Last";

renderer.EvaluateAsNestedProperties = false;

// Act
string result = renderer.Render(new LogEventInfo());

Expand Down
Loading

0 comments on commit 3701d3b

Please sign in to comment.