1

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]&#xD;&#xA;" />
              <footer value="[Footer]&#xD;&#xA;" />
              <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.

Community
  • 1
  • 1
Vincent
  • 1,497
  • 1
  • 21
  • 44
  • How can user delete your App.Config file? – Vivek Nuna Oct 25 '16 at 12:52
  • @viveknuna he goes to the execution path using the file explorer, selects the file and presses delete. – Vincent Oct 25 '16 at 12:53
  • but that should not be allowed – Ghost Developer Oct 25 '16 at 12:54
  • @GhostDeveloper it's his machine, his file system, ofcourse he can delete what ever file he pleases? – Vincent Oct 25 '16 at 12:54
  • You can configure rights to not allow a delete of the content depending on the install path. Also I am not sure a .net 4.5+ app will run without an app.config. If it does run then verify the existence of the app.config file on startup and do not run if it does not exist. – Igor Oct 25 '16 at 12:58
  • 1
    Alternately you can also programmatically configure log4net if you do not detect an app.config file or if there are no loggers configured (app.config has been altered). – Igor Oct 25 '16 at 13:00
  • 1
    The simple solution is to detect the missing config section and either roll with it or terminate the application. You may want to optionally let the end user know that a critical configuration is missing and service X will no be available. Furthermore, you could add complexity and check file versions and checksum and try to pull the latest from a remote server as an exposed update service to your app. I would shy away from making backup copies because you will be in the same predicament once the shadow copies have been removed. – Ross Bush Oct 25 '16 at 13:02
  • @Igor yes I am configuring it in code if i detect the file is not present (it can be run if the file doesn't exist) but i need to save this 'default config' to the app.exe.config so the user can modify it if he wants. – Vincent Oct 25 '16 at 13:02
  • It's XML. Write a new file with your default settings. – Mikanikal Oct 25 '16 at 13:04
  • @Mikanikal quote from my question: "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 this is **A way** but i need a better one – Vincent Oct 25 '16 at 13:05
  • How are you planning on re-loading the app.config at runtime if you re-generate it? I am not sure that is possible... – Igor Oct 25 '16 at 13:06
  • log4net also allows for an external xml file (so not the app.config) and you can monitor that file. This would allow you to re-generate the file if it does not exist. – Igor Oct 25 '16 at 13:07
  • You could save the section as an application resource string and pull it from the executable at runtime and do some funny manipulation with your app config. I would prefer to make the app updateable if I was going to go down that rabbit hole to begin with. – Ross Bush Oct 25 '16 at 13:07
  • There are pretty much 3 options. You pick. You write a new file. You copy a backup, or you just forget about anything in the config file and configure everything in code or in a separate file. Neither is better than the next so to answer your quoted question, no, there is not a "better" way. – Mikanikal Oct 25 '16 at 13:07
  • `I will not reload it, i need to create it and then save it so the file exists again` <= my point is that even when you do this edits will not take effect until you restart the app. Either way there is not really any better way to do this. – Igor Oct 25 '16 at 13:09
  • @Igor oke sorry, that is of no consequence, it only gets used on startup atm, sorry i sould have specified that – Vincent Oct 25 '16 at 13:32

1 Answers1

0

keep a backup file always and use that file when production config is deleted by user.

Ghost Developer
  • 1,283
  • 1
  • 10
  • 18