简体   繁体   中英

log4net cannot find my custom layout class in the class library

I have a console project and a class library. log4net is installed in both my console project and my class library.

I have a layout class like this for testing purposes.

public class CustomLayout : LayoutSkeleton
{
    public override void ActivateOptions() { }

    public override void Format(TextWriter writer, LoggingEvent loggingEvent)
    {
        writer.WriteLine(loggingEvent.MessageObject);
    }
}

This class works fine when I define it in my console project. But when I define this class in the class library, I get an error. log4net does not see this class.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <file value="log-file.txt" />
            <!-- error -->
            <layout type="ConsoleApp1.Core.CustomLayout" />
            <!-- working properly-->
             <!--<layout type="ConsoleApp1.CustomLayout" />--> 
        </appender>
        <logger name="FileLogger">
            <level value="ALL" />
            <appender-ref ref="FileAppender" />
        </logger>
    </log4net>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>

Program.cs

class Program
    {

        private static readonly ILog Log = LogManager.GetLogger("FileLogger");

        static void Main(string[] args)
        {

            Log.Error("This is my error log.");
            Console.ReadLine();
        }
    }

I added this to AssemblyInfo.cs: [assembly: log4net.Config.XmlConfigurator(Watch = true)]

The error I get is as follows:

log4net:ERROR Failed to find type [ConsoleApp1.Core.CustomLayout]
System.TypeLoadException: Could not load type [ConsoleApp1.Core.CustomLayout]. Tried assembly [log4net, Version=2.0.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
   at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
   at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(XmlElement element, Type defaultTargetType, Type typeConstraint)
log4net:ERROR Failed to create object to set param: layout

I tried this but didn't work: <layout type="ConsoleApp1.Core.CustomLayout, ConsoleApp1.Core" />

Since your ConsoleApp1.Core.CustomLayout class is contained in a separate assembly,
you have to you provide the fully qualified name of this layout class.

This includes its namespace, class name and assembly name, which is the name of your compiled library file without the .dll file extension.

For example, when your class library file is called MyClassLibrary.dll (and the namespace and class name being used in your question are correct), the registration of your layout class must look like:

<layout type="ConsoleApp1.Core.CustomLayout, MyClassLibrary" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM