log4net - errors when is called as a Component Service

125 Views Asked by At

I've created a .Net object (it is a basic logger composed by log4net and log4net.Ext.Json). This .Net object is added to Component Services as COM+ Application to make it accessible for some legacy systems.

In order to test this component service, I've created a console application in C# that will create a instance of the object and then will invoke my desired method LogMessage:

static void Main(string[] args)
{
    COMAdminCatalogCollection applications;
    COMAdminCatalog catalog;
        
    catalog = new COMAdminCatalog();
    applications = (COMAdminCatalogCollection)catalog.GetCollection("Applications");
    applications.Populate();

    foreach (COMAdminCatalogObject application in applications)
    {
        if (application.Name.Equals("MyApplicationName"))
        {
            COMAdminCatalogCollection components;
            components = applications.GetCollection("Components", application.Key);
            components.Populate();

            foreach (COMAdminCatalogObject component in components)
            {
                var instance = ComObjectGet(component.Name);

            }
        }
    }
}

public static object ComObjectGet(string componentName)
{
    Type type = Type.GetTypeFromProgID(componentName);
    var COMobject = Activator.CreateInstance(type);
    var COMobjectType = Activator.CreateInstance(type).GetType();
    MethodInfo getAllMethod = COMobjectType.GetMethod("LogMessage");
    object result = getAllMethod.Invoke(COMobject, new object[] { "First param", "Second param", "Third param" });

    return COMobject;
}

When line var COMobject = Activator.CreateInstance(type); is executed, o get this error:

log4net:ERROR Failed to create object to set param: layout log4net:ERROR Failed to find type [log4net.Layout.SerializedLayout, log4net.Ext.Json] System.IO.FileLoadException: Could not load file or assembly 'log4net, Version=2.0.9.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I'm using log4net.Ext.Json because I need to write logs in json format, however, when I modify my log4Net configuration file to use default appender, it works as expected, so my guess is that there is a problem with log4net.Ext.Json. This is my log4Net configuration file:

<configuration>
    <log4net debug="true">
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <file type="log4net.Util.PatternString" value="C:\Logs\MyLog_JSON.log" name ="RollingLogFileAppender" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <countDirection value="-1"/>
            <datePattern value="yyyy-MM-dd"/>
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
                <member value="logDateTime%date:yyyy-MM-dd HH:mm:ss:ffff" />
                <decorator type="log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json" />
                <member value="hostName" />
                <default />
                <remove value="date" />
                <remove value="ndc" />
                <remove value="message" />
                <remove value="thread" />
                <remove value="exception" />
                <member value="logData:messageObject" />
            </layout>
        </appender>
        <root>
            <level value="All"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </root>
    </log4net>
</configuration>

I saw that this issue could be solved by calling log4net.Config.XmlConfigurator.Configure(); but it my case it didn't work and I set log4net in debug mode <log4net debug="true">, as well, but there aren't new clues to find a solution, I just get the same references error.

One thing to mention is that I debug my .Net component directly in Visual Studio and all the things work as expected, the error comes when the object is called as COM+ Application.

0

There are 0 best solutions below