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

Load assembly event #2009

Merged
merged 1 commit into from
Mar 10, 2017
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
58 changes: 58 additions & 0 deletions src/NLog/Config/AssemblyLoadingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright (c) 2004-2016 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.
//

using System;
using System.ComponentModel;
using System.Reflection;

namespace NLog.Config
{
/// <summary>
/// An assembly is trying to load.
/// </summary>
public class AssemblyLoadingEventArgs : CancelEventArgs
{/// <summary>
/// New event args
/// </summary>
/// <param name="assembly"></param>
public AssemblyLoadingEventArgs(Assembly assembly)
{
Assembly = assembly;
}

/// <summary>
/// The assembly that is trying to load.
/// </summary>
public Assembly Assembly { get; private set; }
}
}
26 changes: 25 additions & 1 deletion src/NLog/Config/ConfigurationItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace NLog.Config

/// <summary>
/// Provides registration information for named items (targets, layouts, layout renderers, etc.) managed by NLog.
///
/// Everything of an assembly could be loaded by <see cref="RegisterItemsFromAssembly(System.Reflection.Assembly)"/>
/// </summary>
public class ConfigurationItemFactory
{
Expand All @@ -66,6 +68,11 @@ public class ConfigurationItemFactory

private IJsonSerializer jsonSerializer = DefaultJsonSerializer.Instance;

/// <summary>
/// Called before the assembly will be loaded.
/// </summary>
public static event EventHandler<AssemblyLoadingEventArgs> AssemblyLoading;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationItemFactory"/> class.
/// </summary>
Expand Down Expand Up @@ -205,6 +212,8 @@ public IJsonSerializer JsonSerializer
get { return this.conditionMethods; }
}



/// <summary>
/// Registers named items from the assembly.
/// </summary>
Expand All @@ -221,6 +230,17 @@ public void RegisterItemsFromAssembly(Assembly assembly)
/// <param name="itemNamePrefix">Item name prefix.</param>
public void RegisterItemsFromAssembly(Assembly assembly, string itemNamePrefix)
{
if (AssemblyLoading != null)
{
var args = new AssemblyLoadingEventArgs(assembly);
AssemblyLoading.Invoke(this,args);
if (args.Cancel)
{
InternalLogger.Info("Loading assembly '{0}' is canceled", assembly.FullName);
return;
}
}

InternalLogger.Debug("ScanAssembly('{0}')", assembly.FullName);
var typesToScan = assembly.SafeGetTypes();
PreloadAssembly(typesToScan);
Expand All @@ -233,6 +253,10 @@ public void RegisterItemsFromAssembly(Assembly assembly, string itemNamePrefix)
/// <summary>
/// Call Preload for NLogPackageLoader
/// </summary>
/// <remarks>
/// Every package could implement a class "NLogPackageLoader" (namespace not important) with the public static method "Preload" (no arguments)
/// This method will be called just before registering all items in the assembly.
/// </remarks>
/// <param name="typesToScan"></param>
public void PreloadAssembly(Type[] typesToScan)
{
Expand All @@ -245,7 +269,7 @@ public void PreloadAssembly(Type[] typesToScan)
}

/// <summary>
/// Call Preload method for <paramref name="type"/>. The Preload method must be static.
/// Call the Preload method for <paramref name="type"/>. The Preload method must be static.
/// </summary>
/// <param name="type"></param>
private static void CallPreload(Type type)
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.Xamarin.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.Xamarin.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.doc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.netfx35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.netfx40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.netfx45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.sl4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.sl5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.wp7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.wp71.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.wp8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="Config\AdvancedAttribute.cs" />
<Compile Include="Config\AppDomainFixedOutputAttribute.cs" />
<Compile Include="Config\ArrayParameterAttribute.cs" />
<Compile Include="Config\AssemblyLoadingEventArgs.cs" />
<Compile Include="Config\ConfigSectionHandler.cs" />
<Compile Include="Config\ConfigurationItemCreator.cs" />
<Compile Include="Config\ConfigurationItemFactory.cs" />
Expand Down
74 changes: 70 additions & 4 deletions tests/NLog.UnitTests/Config/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System;
using System.Text.RegularExpressions;
using NLog.Common;
using NLog.Config;
using Xunit.Extensions;

namespace NLog.UnitTests.Config
{
Expand Down Expand Up @@ -371,7 +373,11 @@ public void CustomXmlNamespaceTest()
[Fact]
public void Extension_should_be_auto_loaded_when_following_NLog_dll_format()
{
var configuration = CreateConfigurationFromString(@"
try
{


var configuration = CreateConfigurationFromString(@"
<nlog throwExceptions='true'>
<targets>
<target name='t' type='AutoLoadTarget' />
Expand All @@ -383,8 +389,68 @@ public void Extension_should_be_auto_loaded_when_following_NLog_dll_format()
</rules>
</nlog>");

var autoLoadedTarget = configuration.FindTargetByName("t");
Assert.Equal("NLogAutloadExtension.AutoLoadTarget", autoLoadedTarget.GetType().FullName);
var autoLoadedTarget = configuration.FindTargetByName("t");
Assert.Equal("NLogAutloadExtension.AutoLoadTarget", autoLoadedTarget.GetType().FullName);
}
finally
{
ConfigurationItemFactory.Default.Clear();
ConfigurationItemFactory.Default = null; //build new factory next time
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void Extension_loading_could_be_canceled(bool cancel)
{

EventHandler<AssemblyLoadingEventArgs> onAssemblyLoading = (sender, e) =>
{
if (e.Assembly.FullName.Contains("NLogAutloadExtension"))
{
e.Cancel = cancel;
}
};

try
{
ConfigurationItemFactory.Default = null; //build new factory next time
ConfigurationItemFactory.AssemblyLoading += onAssemblyLoading;

var configuration = CreateConfigurationFromString(@"
<nlog throwExceptions='false'>
<targets>
<target name='t' type='AutoLoadTarget' />
</targets>

<rules>
<logger name='*' writeTo='t'>
</logger>
</rules>
</nlog>");



var autoLoadedTarget = configuration.FindTargetByName("t");

if (cancel)
{
Assert.Null(autoLoadedTarget);
}
else
{
Assert.Equal("NLogAutloadExtension.AutoLoadTarget", autoLoadedTarget.GetType().FullName);
}
}
finally
{
//cleanup
ConfigurationItemFactory.AssemblyLoading -= onAssemblyLoading;
ConfigurationItemFactory.Default.Clear();
ConfigurationItemFactory.Default = null; //build new factory next time

}
}

[Fact]
Expand All @@ -401,7 +467,7 @@ public void Extensions_NLogPackageLoader_should_beCalled()
var fact = ConfigurationItemFactory.Default;

//also throw exceptions
LogManager.Configuration = CreateConfigurationFromString(@"
LogManager.Configuration = CreateConfigurationFromString(@"
<nlog throwExceptions='true'>

</nlog>");
Expand Down