I currently have a app.config which holds the settings for my Log4Net configuration. The problem here is if the user deletes the exe.config then it won't log anything since the logger isn't configured.
apart from the logger there are other things i need to set in the config (in the future). So what I wanted to do is simply write a default exe.config to the place it is supposed to be but i cannot find a way to do this properly.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logging\ModuleSimulator.txt"/>
<appendToFile value="false"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10000000"/>
<staticLogFileName value="false"/>
<rollingStyle value="Size"/>
<PreserveLogFileNameExtension value="true"/>
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]
" />
<footer value="[Footer]
" />
<conversionPattern value="%date [%2thread] %-5level %logger - %message%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<LevelMin value="DEBUG"/>
<AcceptOnMatch value="true"/>
</filter>
</appender>
<root>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
Should i just save this text in code and save it as a document if i notice the original is gone? surely there must be a better way.
I know how to configure my logger in code, so all i need is a way to save it in a new config file: Can you configure log4net in code instead of using a config file?
I detect when there is no config file:
var exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (exeConfiguration.HasFile == false)
{
// TODO: create the config in code
// Save the config in code
}
All i need is a way to save the new config in code so it creates a similar file. I want to know if there is an other way than saving the entire xml file in a string (or resources) and then writing it as a file when I notice the config file is missing.
I am hoping to find a way to use the ConfigurationManager or some other .Net component to configure my application on startup and than use that to do something like: Configuration.Save(); I will accept that all changes in this newly created file will not be used until the application is restarted, that is not a problem.