1

I have used log4net in winforms before. First time using with a wpf console app. The console shows up and the console appender works exactly as in the winforms app. However in winforms I never had to give the full path to the log4net xml file. It is located in the same place where all the cs files are. (the default place where VS 2010 puts all its source files. So

  XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4config.xml"));

works in a winforms app's Program.cs->main() but for the wpf version where I have my own Startup class with my own Main() (instead of the autogenerated and hidden main()) I have to change the line to look like this

  XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"c:\fullpath\log4config.xml"));

This took me quite a while to nail down so I thought it may be worthwhile to figure why this is so. Anyone know ? I am using log4net ver 1.2.10.0. The log4net dll and xml both have copy to local = true in winforms and wpf. thank you

Phil
  • 42,255
  • 9
  • 100
  • 100
Gullu
  • 3,477
  • 7
  • 43
  • 70

3 Answers3

2

Please compare the values of Environment.CurrentDirectory in the two different programs. You will probably find that they are different. The path "log4config.xml" is relative to the current directory, not relative to the program exe.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Environment.CurrentDirectory is same in both wpf and winforms. ie., c:\....\bin\debug or bin\release depending on build type. This is weirder than I thought. thanks – Gullu Mar 19 '12 at 22:37
1

The log4net dll and xml both have copy to local = true.

That syntax applies to the log4net.dll, but for the log4config.xml file, it should have a property called 'Copy to Output Directory' which has options 'Do not Copy', 'Copy if newer' and 'Copy Always'.

When you run it, does the log4config.xml actually exist in the ..\bin\debug or ..\bin\release folder and if so, is it the correct version of the file.?

As Shrieks has said, this is probably not a log4net issue, since log4net should be simply using the FullName property of the FileInfo object that has been created and passed to it.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • 1
    It was set to copy always but VS was not really copying the xml file to the output folder. Thx for pointing me in the right direction. Moderator please delete this if you think my question is a dup. This question explains why this was happening. http://stackoverflow.com/questions/2619547/i-added-a-new-xml-file-to-my-solution-but-when-i-press-f5-to-test-it-i-cant-fi – Gullu Mar 20 '12 at 22:24
  • Basically for copy always to work for xml files, Build action must be set to "Content" – Gullu Mar 20 '12 at 22:26
0

The FileInfo object is resolved before it is passed to that function.

So the fact that it behaves differently in different application types (Winforms & WPF) is a red herring. The main issue is to what value does the Path property of the FileInfo class resolve to.

Shrieks
  • 141
  • 1
  • 6
  • FileInfo takes relative filename or fqn. However for winforms it appears to look relatively from the right spot. thx – Gullu Mar 19 '12 at 22:40