I have a requirement to store my log files in a Logs sub folder. Normally you would do this my modifying the FileAppender in you App.config as follows:
<file type="log4net.Util.PatternString" value="LogsMyLog.log" />
However this is not cross platform because if you run this on linux it will create a file called ‘LogsMyLog.log’ rather then a directory. This is because ” is not a path separator on linux.
We could change it to ‘/’ but that would only work on linux and not on windows.
How can you use the XML config for log4net to put my logs in a subfolder that works cross platform?
Advertisement
Answer
A possible solution can be that you configure the file for windows and than after configuration test if you are on Linux. When on Linux you can get the appender and change the file=>value value with a replace to be Linux compatible:
XmlConfigurator.Configure(); if (Environment.OSVersion.Platform == PlatformID.Unix){ var repository = LogManager.GetRepository() as Hierarchy; if (repository != null) { var appenders = repository.GetAppenders(); if (appenders != null) { foreach (var appender in appenders) { if (appender is FileAppender) { var fileLogAppender = appender as FileAppender; fileLogAppender.File = fileLogAppender.File.Replace (@"", Path.DirectorySeparatorChar.ToString ()); fileLogAppender.ActivateOptions (); } } } } }