I've programmed a C# app on Windows and have now deployed it on Linux. When I start the app via terminal as a normal (no sudo) user, the log4net works correctly. But when I set the app to run as a systemd, the app is working but the logging function is not.
Here my log4net.config:
<log4net>
<root>
<level value="INFO" />
</root>
<!-- myApp -->
<logger name="myApp">
<level value="DEBUG" />
<appender-ref ref="app" />
</logger>
<appender name="app" type="log4net.Appender.RollingFileAppender">
<file value="/var/client_name/log/myApp_v001/app.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline" />
</layout>
</appender>
<!-- ExternalLibrary -->
<logger name="ExternalLibrary">
<level value="WARN" />
<appender-ref ref="extLib" />
</logger>
<appender name="extLib" type="log4net.Appender.RollingFileAppender">
<file value="/var/client_name/log/myApp_v001/extLib.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
On my C# app I'm setting the XmlConfigurator:
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
On windows, while debugging the log4net saves the log files under c:/var/client_name/log/myApp_v001/*
Then I published the app for Linux and deployed it. On the Linux machine I've created a myApp.service file under /etc/systemd/system with following content:
[Unit]
Description=myApp Service
After=network.target remote-fs.target
[Service]
Type=simple
ExecStart=/opt/dotnet/dotnet /opt/client_name/myApp_v001/myApp.dll
ExecStartPre=/bin/sleep 10
Restart=always
User=normaluser
Group=normaluser
[Install]
WantedBy=multi-user.target
Then restarted systemctl and started the service manually:
sudo systemctl start myApp.service
The app runs and is working, but the log files are not being created.
Then I stopped the service
sudo systemctl stop myApp.service
And started the app without systemd:
cd /opt/client_name/myApp_v001/
dotnet myApp.dll
The app runs and the log files are being created accordingly.
The rights of the folder containing the logs are set to
drwxrw-rw- 2 normaluser normaluser
How can I make the logger to work also when being started by a systemd?
Used hardware/software:
Linux = Raspbian GNU/Linux 9 (stretch)
.Net-Core = Microsoft.NETCore.App 3.1.9
I got log4net to work with dotnet core systemd services by making the Log path relative.
I also believe that you may want to specify a WorkingDirectory in your Unit File. That way if you configure Log4Net's file appender to log to Logs/Service.log.txt, then you should see a Logs folder created in the $WorkingDirectory.
The other option is to use the built in journalctl to view logs.