Reputation: 553
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
Upvotes: 0
Views: 570
Reputation: 12276
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.
Upvotes: 1