0

So i'm in need to install log4net package for logging. this works well no problem but it doesn't seem to put the right configuration also i doesn't recognize log4net child element in the app.config file.

I followed this link/ thread log4net

I already added the assembly in the properties file and the configsections in the app.config file. I also do seem to find the package in the packages config which seems right the me. But in other projects we have that uses log4net i also see a runtime section with assembly name and public token.

I install nuget package explorer but it only opens nugetpkg files or something while referenece and stuff are dll libraries. Beyond the way to do this it doesn't seem very easy to do the configuration as normally when you install a package it will put in all the configuration for you.

Anyway, is there something i'm doing wrong and if not what should I do next?

as for the public token I also tried sn.exe -T assembly but when I type log4net or Log4Net of Log4net itdoesn't seem to find the assembly

  • Did you set the file properties as content, copy always? – Andy May 20 '20 at 15:09
  • And how did you add log4net itself? I don't follow your explanation. You should just need to add the nuget package and a config. – Andy May 20 '20 at 15:10
  • @Andy i did add the nuget package via the nuget package manager yes, but but what about the config? Normally fr example when I install entityframework via nuget it install the package and runtime in the app.config file while log4net did not. – Technology Researcher May 22 '20 at 06:26
  • It doesn't give you any config file. You need to add one yourself. And make sure it'll be copied into your bin when you run. – Andy May 22 '20 at 07:04
  • @Andy the last part i do not understand at this point i do have a configuration but just in the app.config altough it does not make a log file probably coz the last thing-> What should I copy to the bin directory and i assume you mean debug/bin directory? – Technology Researcher May 22 '20 at 09:14

1 Answers1

1

When you use Log4net you're best adding a separate config file specifically for log4net. This is a different file in addition to any app.config you may be using.

You can technically use a json file for config and you might be able to add a section to app.config. I never tried the latter.

Don't do either of those.

There are a huge and bewildering load of different settings and you're best keeping things simple.

That's not the only reason I work this way though.

This approach will also work for all options. When I do asp.net core or a console app or anything else then I'm using the same option.

In any case, leave complications for later once you get it working.

You add the log4net nuget package ( which I gather you've done ).

That gives you the dll and references them.

Nothing will happen if that's all you do though.

Because it's the config file tells it what to do with what log levels and nothing will happen until log4net in the exe reads those settings.

You then add a log4net.config file.

This must go in the root folder of your entry point project. That's so it will end up in your bin folder next to the exe.

There are a bunch of example configs out there on the internet you could copy. Here's one.

If you just add that config file to your project then nothing will work because when you compile and run the exe won't find that file on disk where it's looking.

You need to get the build process to copy that file.

The way you do that is to set 2 properties on it.

Select the file in solution explorer.

With a default visual studio set up, beneath solution explorer you have properties.

Set Build Action: Content Copy to output Directory: Copy if newer.

Build your project.

Go look in your bin and find your compiled exe.

Next to it should be a copy of your new config file.

So long as your code writing log entries is good and the folders that config file references are accessible then it should work.

EDIT:

If it still doesn't find the config then tell it explicitly which config file to go read. You do that by adding the following line:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

To your assemblyinfo.cs file. That's under properties in a net core app.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • thanx I already have this part, what about the runtime? i see in some other applications we have there is a runtime section with assembly binding for log4net is this necessary too? And how do i get to the public token, i have nuget package manager explorer but it cannot open dll files – Technology Researcher May 22 '20 at 14:33
  • 1
    I think it should automatically find the config file but you can explicitly tell it what file to look for. I added a bit under Edit. AFAIK You should only need assembly binding if you have a compiled dll that relies on a specific old version. I don't follow what you're saying with tokens and nuget package manager explorer. You add the nuget package, maybe force package install with a "Update-Package -reinstall" in the package manager console and that's all. The dll will be there, referenced and usable. No tokens. – Andy May 22 '20 at 14:57
  • I already added the assembly, at this point i still don't have any log files tough, but when I'm fully finished and released and attached to the cms i'll see again what the output will be i just guess its because i'm still debugging and testing thx a lot – Technology Researcher May 22 '20 at 15:11