Log4net set path

Dani_S 2,726 Reputation points
2022-05-11T06:45:02.067+00:00

Hi,
I used log4net for worker service and Maui app and I want to keep the log file near the exe, how is can be done ?
Thanks in advance,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>

<log4net>

<appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <file type="log4net.Util.PatternString" value="%property{LogFileName}.txt" />
    <file value="C:\Temp\service.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="25MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
</appender>
    <root>
        <level value="INFO" />
        <!-- Please Look at Levels bellow, level OFF will log only OFF, level FATAL will log OFF and FATAL and so on...
ALL    DEBUG   INFO    WARN    ERROR   FATAL   OFF

•All •DEBUG •DEBUG •INFO •INFO •INFO •WARN •WARN •WARN •WARN •ERROR •ERROR •ERROR •ERROR •ERROR •FATAL •FATAL •FATAL •FATAL •FATAL •FATAL •OFF •OFF •OFF •OFF •OFF •OFF •OFF

        <!--<appender-ref ref="console" />-->
        <appender-ref ref="file" />
    </root>

</log4net>
</configuration>

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,900 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2022-05-11T20:49:48.82+00:00

    see the docs:

    https://logging.apache.org/log4net/release/manual/configuration.html

    in the <appender name="file" type="log4net.Appender.RollingFileAppender"> section,

    there should only be one <file> entry. it defines the path to the file. you can use the config base path, environment variables, or global properties to set where it is located.