question

01195804 avatar image
0 Votes"
01195804 asked LeonLu-MSFT commented

Log4net set path

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>

dotnet-runtimedotnet-maui
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered LeonLu-MSFT commented

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.




· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have one log 4net configuration for worker service and one for client MAUI.
I read the doc and didn't understand how I set the path where file is located, using the config base path, environment variables, or global properties to set where it is located.
Could you please give sniped code?

0 Votes 0 ·

For worker service I used <file value="service.log" /> and it run near exe.
But MSIX of MAUI where it run the log file when I used the same technic?

0 Votes 0 ·

depends on the os.

for linux you should create a folder under /var/log. the installer should create the folder as it require su permission

on MacOs you create a folder under ~/Library/Logs. as apps are installed in the Application folder, they will not have write access to the folder.

on windows there s no standard as the event log is typically used. probably a folder under %APPDATA%


0 Votes 0 ·
Show more comments