xmlpostgresqlasp.net-corelogginglog4net

log4net:ERROR ConfigureFromXml called with null 'element' parameter for Postgres SQL


when we are trying to insert log files in the postgres sql we are facing the issue:

log4net:ERROR ConfigureFromXml called with null 'element' parameter.

In this code we are trying to insert log files in postgres SQL using Asp.net core.

Under configservice() code in startup.cs file:

return services.AddAbp<DTSWebHostModule>(options =>
{
    //Configure Log4Net logging
    options.IocManager.IocContainer.AddFacility<LoggingFacility>(
        f => f.UseAbpLog4Net().WithConfig(_hostingEnvironment.IsDevelopment()
            ? "log4net.config"
            : "log4net.Production.config")

    );

});

code in log4net.config file:

<configuration>

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
            <connectionType value="Npgsql.NpgsqlConnection, Npgsql" />
            <connectionString value="User ID=postgres;Password=password;Host=localhost;Port=5432;Database=DTSPowerToolsPostgres;Pooling=true;" />
            <commandText value="INSERT INTO public.log ( thread, log_level, logger, message, exception) 
                      VALUES ( @thread, @log_level, @Source, @message, @exception)" />
            <commandType value="Text" />

            <bufferSize value="1" />

            <lossy value="false" />

            <parameter>
                <parameterName value="@thread" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%thread" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@log_level" />
                <dbType value="String" />
                <size value="50" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%level" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@Source" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%logger" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@message" />
                <dbType value="String" />
                <size value="4000" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%message" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@exception" />
                <dbType value="String" />
                <size value="2000" />
                <layout type="log4net.Layout.ExceptionLayout" />
            </parameter>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <appSettings>
                <add key="log4net.Internal.Debug" value="true"/>
            </appSettings>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %-5level %logger - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="AdoNetAppender" />
        </root>
    </log4net>

</configuration>

Check for another error causing here:

This code is in log4net.config file tried using below code for datetime parameter to insert logs into DataBase.

In log4net.config File:

1) <parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawUtcTimeStampLayout" />

This code works and inserts Logs into DB. But, an exception mentioned in the screenshot is appearing.

2) <parameter>
<parameterName value="@log_date" />
<dbType value="TimestampTz" /> <!-- PostgreSQL timestamp with time zone -->
<layout type="log4net.Layout.RawUtcTimeStampLayout" />

This code didn't work and not inserting Logs into DB. But, No exception is appaering.

3) <parameter>
<parameterName value="@log_date" />
<dbType value="Timestamp" /> <!-- PostgreSQL timestamp without time zone -->
<layout type="log4net.Layout.RawUtcTimeStampLayout" />

This code didn't work and not inserting Logs into DB. But, No exception is appaering.

screenshot, Click Here..

This screenshot says that, The Postgres SQL uses TimeStamp with Time Zone but, we are using "DateTime" dbType with of layout type Kind=UTC. which, the DateTime can't be written with different types

But in the first code, we are making use of DateTime which is .NET object which is causing the error and inserting Logs into DB.

In the Second code, we are making use of TimestampTz which is Postgre SQL DB type which is not causing the error and not inserting Logs into DB.

For the Third code, which is Same issue as Second code(above).

Note: If there is no DateTime parameter in log4net.config file also causing the same exception as in screenshot.

The following Script is for Postgres SQL table creation:

CREATE TABLE log(
id SERIAL PRIMARY KEY,
log_date TIMESTAMP WITH TIME ZONE NOT NULL,
thread VARCHAR(255),
log_level VARCHAR(50),
logger VARCHAR(255),
message TEXT,
exception TEXT

);

Hope, this helps you understand the problem. We are unable to share the code using new repo. Thank you.


Solution

  • The root node in the log4net.config configuration file is <log4net></log4net>. So you need to modify the log4net.config file as follows.

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- Log4net Logging Setup -->
    <log4net>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
            <connectionType value="Npgsql.NpgsqlConnection, Npgsql" />
            <connectionString value="User ID=postgres;Password=password;Host=localhost;Port=5432;Database=DTSPowerToolsPostgres;Pooling=true;" />
            <commandText value="INSERT INTO public.log ( thread, log_level, logger, message, exception) 
                      VALUES ( @thread, @log_level, @Source, @message, @exception)" />
            <commandType value="Text" />
    
            <bufferSize value="1" />
    
            <lossy value="false" />
    
            <parameter>
                <parameterName value="@thread" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%thread" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@log_level" />
                <dbType value="String" />
                <size value="50" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%level" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@Source" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%logger" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@message" />
                <dbType value="String" />
                <size value="4000" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%message" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@exception" />
                <dbType value="String" />
                <size value="2000" />
                <layout type="log4net.Layout.ExceptionLayout" />
            </parameter>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <appSettings>
                <add key="log4net.Internal.Debug" value="true"/>
            </appSettings>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %-5level %logger - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="AdoNetAppender" />
        </root>
    </log4net>