Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to NLog 5.0 preview 2 #701

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ deploy:
test_script:
- nuget.exe install OpenCover -ExcludeVersion -DependencyVersion Ignore
- OpenCover\tools\OpenCover.Console.exe -register:user -returntargetcode -target:"%xunit20%\xunit.console.x86.exe" -targetargs:"\"c:\projects\nlogweb\tests\NLog.Web.Tests\bin\Release\net452\NLog.Web.Tests.dll\" -appveyor -noshadow" -returntargetcode -filter:"+[NLog.Web]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -oldstyle -output:coverage.xml
- OpenCover\tools\OpenCover.Console.exe -register:user -returntargetcode -target:"%xunit20%\xunit.console.x86.exe" -targetargs:"\"c:\projects\nlogweb\tests\NLog.Web.Tests\bin\Release\net461\NLog.Web.Tests.dll\" -appveyor -noshadow" -returntargetcode -filter:"+[NLog.Web]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -oldstyle -mergeoutput -output:coverage.xml
- OpenCover\tools\OpenCover.Console.exe -register:user -returntargetcode -target:"%xunit20%\xunit.console.x86.exe" -targetargs:"\"c:\projects\nlogweb\tests\NLog.Web.AspNetCore.Tests\bin\Release\net461\NLog.Web.AspNetCore.Tests.dll\" -appveyor -noshadow" -filter:"+[NLog.Web]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -oldstyle -mergeoutput -output:coverage.xml
- pip install codecov
- codecov -f "coverage.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\..\..\..\packages\NLog.4.5.8\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="NLog" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net461" />
<package id="NLog" version="4.6.2" targetFramework="net461" />
<package id="NLog" version="5.0.0-preview.2" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
<package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace NLog.Web.LayoutRenderers
/// </summary>
[LayoutRenderer("aspnet-environment")]
[ThreadAgnostic]
[ThreadSafe]
public class AspNetEnvironmentLayoutRenderer : LayoutRenderer
{
private static IHostEnvironment _hostEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace NLog.Web.LayoutRenderers
/// ASP.NET User ClaimType Value Lookup.
/// </summary>
[LayoutRenderer("aspnet-user-claim")]
[ThreadSafe]
public class AspNetUserClaimLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
19 changes: 13 additions & 6 deletions src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
<Product>NLog.Web.AspNetCore v$(VersionPrefix)</Product>
<Description>
NLog LoggerProvider for Microsoft.Extensions.Logging and ASP.NET Core platform. Adds helpers and layout renderers for websites and web applications.
Expand Down Expand Up @@ -30,7 +30,7 @@ See also https://github.com/NLog/NLog.Web/releases
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/NLog/NLog.Web</RepositoryUrl>
<SignAssembly>true</SignAssembly>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<RootNamespace>NLog.Web</RootNamespace>
Expand All @@ -56,12 +56,16 @@ See also https://github.com/NLog/NLog.Web/releases
<Title>ASP.NET Core 2 integration for NLog - .NET Standard 2</Title>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<Title>ASP.NET Core 3 integration for NLog - .NET Core 3</Title>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<Title>ASP.NET Core 3 integration for NLog - .NET Core 3.1</Title>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<Title>ASP.NET Core 5 integration for NLog - .NET 5.0</Title>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.0.0-preview.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461' ">
<!-- Fixed to 2.1.0 as 2.1 is Long Term Supported (LTS) and works with vanilla .NET Core 2.1 SDK -->
Expand All @@ -72,7 +76,10 @@ See also https://github.com/NLog/NLog.Web/releases
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
Expand Down
155 changes: 155 additions & 0 deletions src/NLog.Web/Internal/LogEventInfoBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//
// Copyright (c) 2004-2021 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.Web.Internal
{
using NLog.Common;
using System;

/// <summary>
/// A cyclic buffer of <see cref="LogEventInfo"/> object.
/// </summary>
internal class LogEventInfoBuffer
{
private readonly object _lockObject = new object();
private readonly bool _growAsNeeded;
private readonly int _growLimit;

private AsyncLogEventInfo[] _buffer;
private int _getPointer;
private int _putPointer;
private int _count;

/// <summary>
/// Initializes a new instance of the <see cref="LogEventInfoBuffer" /> class.
/// </summary>
/// <param name="size">Buffer size.</param>
/// <param name="growAsNeeded">Whether buffer should grow as it becomes full.</param>
/// <param name="growLimit">The maximum number of items that the buffer can grow to.</param>
public LogEventInfoBuffer(int size, bool growAsNeeded, int growLimit)
{
_growAsNeeded = growAsNeeded;
_buffer = new AsyncLogEventInfo[size];
_growLimit = growLimit;
_getPointer = 0;
_putPointer = 0;
}

/// <summary>
/// Gets the capacity of the buffer
/// </summary>
public int Size => _buffer.Length;

/// <summary>
/// Gets the number of items in the buffer
/// </summary>
internal int Count { get { lock (_lockObject) return _count; } }

/// <summary>
/// Adds the specified log event to the buffer.
/// </summary>
/// <param name="eventInfo">Log event.</param>
/// <returns>The number of items in the buffer.</returns>
public int Append(AsyncLogEventInfo eventInfo)
{
lock (_lockObject)
{
// make room for additional item
if (_count >= _buffer.Length)
{
if (_growAsNeeded && _buffer.Length < _growLimit)
{
// create a new buffer, copy data from current
int newLength = _buffer.Length * 2;
if (newLength >= _growLimit)
{
newLength = _growLimit;
}

InternalLogger.Trace("Enlarging LogEventInfoBuffer from {0} to {1}", _buffer.Length, newLength);
var newBuffer = new AsyncLogEventInfo[newLength];
Array.Copy(_buffer, 0, newBuffer, 0, _buffer.Length);
_buffer = newBuffer;
}
else
{
// lose the oldest item
_getPointer = _getPointer + 1;
}
}

// put the item
_putPointer = _putPointer % _buffer.Length;
_buffer[_putPointer] = eventInfo;
_putPointer = _putPointer + 1;
_count++;
if (_count >= _buffer.Length)
{
_count = _buffer.Length;
}

return _count;
}
}

/// <summary>
/// Gets the array of events accumulated in the buffer and clears the buffer as one atomic operation.
/// </summary>
/// <returns>Events in the buffer.</returns>
public AsyncLogEventInfo[] GetEventsAndClear()
{
lock (_lockObject)
{
int cnt = _count;
if (cnt == 0)
return new AsyncLogEventInfo[0];

var returnValue = new AsyncLogEventInfo[cnt];

for (int i = 0; i < cnt; ++i)
{
int p = (_getPointer + i) % _buffer.Length;
var e = _buffer[p];
_buffer[p] = default(AsyncLogEventInfo); // we don't want memory leaks
returnValue[i] = e;
}

_count = 0;
_getPointer = 0;
_putPointer = 0;

return returnValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-application")]
[ThreadSafe]
public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<TargetFrameworks>net35</TargetFrameworks>
<TargetFrameworks>net35;net46</TargetFrameworks>
<Product>NLog.Web v$(VersionPrefix)</Product>
<Description>
NLog logging for ASP.NET and ASP.NET MVC. Includes targets and layout renderers for websites and web applications.
Expand Down Expand Up @@ -33,7 +33,7 @@ See https://github.com/NLog/NLog.Web/releases
<RepositoryUrl>https://github.com/NLog/NLog.Web.git</RepositoryUrl>

<SignAssembly>true</SignAssembly>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -49,7 +49,7 @@ See https://github.com/NLog/NLog.Web/releases
<DownloadFile SourceUrl="https://nlog-project.org/N.png" DestinationFolder="$(MSBuildThisFileDirectory)" />
</Target>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.11" />
<PackageReference Include="NLog" Version="5.0.0-preview.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
Expand Down
11 changes: 6 additions & 5 deletions src/NLog.Web/Targets/Wrappers/AspNetBufferingTargetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NLog.Common;
using NLog.Targets;
using NLog.Targets.Wrappers;
using NLog.Web.Internal;

namespace NLog.Web.Targets.Wrappers
{
Expand Down Expand Up @@ -163,7 +164,7 @@ protected override void CloseTarget()
/// <param name="logEvent">The log event.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
LogEventInfoBuffer buffer = GetRequestBuffer();
var buffer = GetRequestBuffer();
if (buffer != null)
{
WrappedTarget.PrecalculateVolatileLayouts(logEvent.LogEvent);
Expand All @@ -178,27 +179,27 @@ protected override void Write(AsyncLogEventInfo logEvent)
}
}

private LogEventInfoBuffer GetRequestBuffer()
private NLog.Web.Internal.LogEventInfoBuffer GetRequestBuffer()
{
HttpContext context = HttpContext.Current;
if (context == null)
{
return null;
}

return context.Items[dataSlot] as LogEventInfoBuffer;
return context.Items[dataSlot] as NLog.Web.Internal.LogEventInfoBuffer;
}

private void OnBeginRequest(object sender, EventArgs args)
{
InternalLogger.Trace("Setting up ASP.NET request buffer.");
HttpContext context = HttpContext.Current;
context.Items[dataSlot] = new LogEventInfoBuffer(BufferSize, GrowBufferAsNeeded, BufferGrowLimit);
context.Items[dataSlot] = new NLog.Web.Internal.LogEventInfoBuffer(BufferSize, GrowBufferAsNeeded, BufferGrowLimit);
}

private void OnEndRequest(object sender, EventArgs args)
{
LogEventInfoBuffer buffer = GetRequestBuffer();
var buffer = GetRequestBuffer();
if (buffer != null)
{
InternalLogger.Trace("Sending buffered events to wrapped target: {0}.", WrappedTarget);
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/Internal/PropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ private static object GetValueAsNestedProperties<T>(string key, T container, Fun
var value = getVal(container, path?.First() ?? key);
if (value != null && path?.Length > 1)
{
foreach (var property in path.Skip(1))
for (int i = 1; i < path.Length; ++i)
{
var propertyInfo = GetPropertyInfo(value, property);
var propertyInfo = GetPropertyInfo(value, path[i]);
value = propertyInfo?.GetValue(value, null);
if (value == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace NLog.Web.LayoutRenderers
#endif
[LayoutRenderer("aspnet-appbasepath")]
[ThreadAgnostic]
[ThreadSafe]
public class AspNetAppBasePathLayoutRenderer : LayoutRenderer
{
#if ASP_NET_CORE
Expand Down
3 changes: 1 addition & 2 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-item")]
[ThreadSafe]
public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down Expand Up @@ -84,7 +83,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

var context = HttpContextAccessor.HttpContext;
var value = PropertyReader.GetValue(Variable, context?.Items, LookupItemValue, EvaluateAsNestedProperties);
var value = PropertyReader.GetValue(Variable, context?.Items, (items, key) => LookupItemValue(items, key), EvaluateAsNestedProperties);
var formatProvider = GetFormatProvider(logEvent, Culture);
builder.Append(Convert.ToString(value, formatProvider));
}
Expand Down
1 change: 0 additions & 1 deletion src/Shared/LayoutRenderers/AspNetMvcActionRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-mvc-action")]
[ThreadSafe]
public class AspNetMvcActionRenderer : AspNetMvcLayoutRendererBase
{
/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/Shared/LayoutRenderers/AspNetMvcControllerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-mvc-controller")]
[ThreadSafe]
public class AspNetMvcControllerRenderer : AspNetMvcLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-contenttype")]
[ThreadSafe]
public class AspNetRequestContentTypeLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
Loading