Log4Net not working for console app - log4net

I have a console app and I am trying to implement log4Net for it.
I did the following steps -
added log4Net reference
Created Log4Net.config -
Created the LogHelper.cs class -
Added the following to the AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
But the logging doesnt seem to work? Can someone suggest what needs to be done for the logging to work in th console app?

Make sure the config file is set to copy to the output directory.
In the log4net documentation for assembly attributes it says:
".. if you use configuration attributes you must invoke log4net to
allow it to read the attributes. A simple call to LogManager.GetLogger
will cause the attributes on the calling assembly to be read and
processed. Therefore it is imperative to make a logging call as
early as possible during the application start-up, and certainly
before any external assemblies have been loaded and invoked."
If it still doesn't work, enable log4net debugging as in this answer

Related

How can I suppress the initial StatusLogger error message

I am using Apache POI 5.2.2 in my application. Apache POI is using log4j2 as it's internal logging mechanism for debugging. As a result log4j-api-2.17.2.jar is a required dependency. I should note here that I am not using maven, so I downloaded the POI binary distribution and include the required dependencies in the application class path.
The logging in POI is intended for POI developers and it is not recommended for normal operation (i.e. end users). For my use case I do not want to use logging at all. However, when my application first loads one of the POI classes the following message is printed on stderr:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
I don't want to add log4j-core, because I really don't want the logging. I really just want to suppress this message, because logging is irrelevant to the app. I would like to do this programmatically so as to avoid adding a config file for something that I am not using. I tried to do this by setting the log4j2.StatusLogger.level property to OFF but this did not make any difference (I also tried setting it to 0).
Is there a simple incantation that I can use to suppress the StatusLogger message? Or do I have to fully get in to the log4j2 world and code up a null logger?
You can disable the message by explicitly setting the logger context factory to org.apache.logging.log4j.simple.SimpleLoggerContextFactory in one of the Log4j2 property sources.
You can, e.g., add:
log4j2.loggerContextFactory = org.apache.logging.log4j.simple.SimpleLoggerContextFactory
to a log4j2.component.properties file on your classpath.
Add to the command line flags:
-Dlog4j2.loggerContextFactory=org.apache.logging.log4j.simple.SimpleLoggerContextFactory
This factory will set SimpleLogger by default.

Load Log4net custom appender from a library outside the application folder

I have a C# custom appender library which needs to be used by several applications in different solutions which run in the same server. I have an addon installer solution that creates all the folders and stuffs which includes the custom appender library, which the other projects needs to use. Instead of adding this library into all the application's directory, I just want to let the each application's App.Config to point to this single custom appender library.
I have built a custom appender call MyCustomAppender in a library call MyCustomLibrary.dll.
Here is the xml code of one of the App.Configs:
<appender name="MyCustomAppender" type="MyCustomerLibrary.MyCustomAppender, C:\Users\Admin\Libraries\MyCustomLibrary">
It is not possible for me to put the path of the library because Log4net doesn't work this way. Is there a walk around for each application to point to C:\Users\Admin\Libraries\MyCustomLibrary.dll? to use the MyCustomAppender? The other applications needs to have 0 code changes, only their App.Config can be modified due the the reason this process needs to have minimal changes.
You can add the dll to the GAC on the machine your application is runnen. This will allow all programs to find the dll. BTW, coping the dll into the bin directory of the application will not change the code of the application.

How to take binding information from outside App.config in C# console application?

I have an App.config fiel in which i defined some binding information.
I want this information in some external file so that user can change it whenever required.
Anyone have an idea about how to implement it?
Thanks in advance
Assuming we're talking about assembly binding redirects...
The runtime is going to use your App.exe.config file to locate the binding redirects. I don't believe you can do anything about this in a normal managed application because the runtime is already initialized by the time you can run any code.
If you had an unmanaged "shim" (an exe in C++ for example) that used the CLR hosting API's, you can tell it the name of a config file to use when you set up the AppDomain. But I don't believe you can make your binding redirects separate from the main application config.
Your only other option would be to handle the AppDomain.AssemblyResolve event to handle binding failures. But this is potentially expensive as the runtime will exhaust all of its options for probing the assembly before raising this event.

Using log4net in a complex software

I'm using log4net logging in my software that consists of several applications.
I want to have one common library for this.
I created a library and put it in the conficuration file. In AssemblyInfo.cs placed attribute:
log4net.Config.XmlConfigurator(ConfigFile = #"c:\logging.xml", Watch = true)
It work for windows service, but in dosn't work for asp.net application.
It work in asp.net if delete attribute from common library and put in into global.asax. However, this leads to that section of the log4net configuration must be made in the windows service.
There is also a business process which causes our library through the
remouting. I want the logging was carried out there too.
Is there way around this?
In my opinion the library should not define where the configuration file is found. Maybe a better idea would be to have a helper method that allows you to configure log4net quickly; that method would take an optional parameter for the config file path and would try to load the configuration file from the specified path first and if that does not work fallback to some maybe the current folder, the application folder or even the web / app.config.
If you insist that it must be an absolute path then you need to give the IIS Application Pool user read access to this file. This way the configuration by attribute should work for services and ASP.Net applications. I do not understand what you mean by "remounting".

Linqpad and Log4net

I have linqpad referencing one of my own assemblies which uses log4net. When linqpad calls my assembly method I am setting my log4net logging levels to ERROR yet I see debug level messages from my assembly showing up in the linqpad results area.
Anybody know what causes this? Does Linqpad use log4net itself or have any special behavior with log4net?
LINQPad uses SharpDevelop which has a dependency on log4net, but this is kept pretty well isolated to the UI domain and shouldn't have any effect on your queries.
How are you configuring log4net? Is it programmatically or via an application config file? If it's the latter, the application config file should be named linqpad.config in order to be picked up by your queries.
I was having trouble with my configuration not being picked up. Creating a separate Linqpad.config (not editing LINQPad's own!) is the key bit of info I was missing - thanks Joe. Fantastic app by the way!

Resources