I am using Log4Net v1.2.13.0 with .Net 4.5. It is logging just fine when I run my code as a console application. However, when I run it as a service I get nothing. The service is running as my login with admin permissions. When I attach to the process and step through the code in debug mode it is as if nothing in the config file has even been read. When you check the logger object none of the properties are set true, e.g. Logger.IsDebugEnabled, Logger.IsInfoEnabled etc.
Here is how I am configuring a logger instance and then retrieving the same instance.
private static readonly ILog Logger = LogManager.GetLogger(typeof(ServiceHostService));
I am setting the logging level to ALL.
Any ideas on what might be wrong?
If LogManager.GetRepository().Configured returns false, then log4net has not been initialised.
Assuming you have your config in XML, will need either a call to log4net.Config.XmlConfigurator.Configure(…) or an assembly attribute like [assembly: log4net.Config.XmlConfigurator(…)] to load the config. If it's in a separate file from the app.config, make sure that file is copied to the output.
If log4net is configured but all log levels are still disabled, check for configuration issues by looking at the results of LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>().
It may help if you add your config to your question.
Related
To log a piece of information like errors, information, warnings and many more we used log4NET library and logging in a local file. But our new requirement, Along with file we need to log this information into Azure Application Insights.
We are working with .NET C# Library,
Is really required ApplicationInsights.config file with .NET C# Project? If not what is the best approach to solving this problem.
Is caching applicationInsights.config instrumentKey? if yes then what will be the solution?
Scenario 1: Tried without adding ApplicationInsights.config file
Output: Log4Net logs are not adding in Azure Application Insights
Scenario 2: Tried with adding ApplicationInsights.config file
Output: Log4Net logs are coming
Scenario 3: When I add Nuget API for ApplicationInsights.Log4NetAppender, It creates log4Net section and includes AIAppender. We have separate log4Net.config and it has only File Appender. We want to use only log4Net.config and remove log4net section from App.Config file.
Output: I removed log4Net section from App.Config and added it in log4Net.config and tested but logs are not adding in Application Insights.
Scenario 4: Implemented TelemetryClient through C# code with ApplicationInsights.config logs are coming in Application Insights
TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.InstrumentKey =<Your Instrument Key>;
// To test
telemetryClient.TrackTrace("Test through code");
The configuration of Application Insights can be done by any combination of config file and code: only the one, or the other, or both of them.
If you find an ApplicationInsights.config file in your project, it was most probably created by installing one of the Application Insights (AI) Nuget packages, but you can remove it and make the same configuration by code.
To allow the mix of configuration by config file and code, the AI clients and configuration classes have GetDefault() or Active static members that give access to an object which can be used globally. For example, you can see that the Log4Net appender TelemetryClient.cs
uses the active telemetry configuration:
public TelemetryClient() : this(TelemetryConfiguration.Active)
If you check the docs for TelemetryConfiguration.Active you'll see this:
Gets the active TelemetryConfiguration instance loaded from the ApplicationInsights.config file. If the configuration file does not exist, the active configuration instance is initialized with minimum defaults needed to send telemetry to Application Insights.
NOTE: Things are a bit different depending on the platform. For example in .NET Core is more advisable to use dependency injection. Have a look under at your case under Code-base monitoring.
For example, if you have a configuration file like this:
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3</InstrumentationKey>
this would take the instrumentation key from it:
_configuration = TelemetryConfiguration.CreateDefault();
If you remove the config file, this will not fail, but you'll need to do set the instrumentation key in code:
_configuration.InstrumentationKey = "exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3";
If your config file include intializers, modules or whatever, you can also configure them by code. For example:
<TelemetryInitializers>
<Add Type="...MyInitializer, ..." />
</TelemetryInitializers>
can be setup in code like this:
_configuration.TelemetryInitializers.Add(new MyInitializer(...));
I am integrating NLog logging in a WCF service hosted in IIS 7.5. Multiple instances (different versions) of the service are installed under the same site, so I would like the Application name to be part of the logging path.
I am aware of the NLog.Web extension and the ${iis-site-name} but since the site is common to all instances it is the application name below site level I would like to use in the log file path.
Each of the applications are installed from separate baseDirs but I would prefer to have logs in a folder structure away from the baseDir.
Any hints to getting the application name? When searching for answers most threads leads to HostingEnvironment.SiteName but I have not been able to find similar ways to get the application name.
Thanks in advance.
You can use the HostingEnvironment.ApplicationVirtualPath property and then hookup an NLog layout renderer.
This should work for NLog 4.4 and newer:
LayoutRenderer.Register("iis-application-name", (logEvent) => HostingEnvironment.ApplicationVirtualPath);
Then you can use ${iis-application-name} in your NLog config file.
Just remember to register the renderer before doing anything else, that is do it before you start loggin.
.Net applications are set up so that multiple instances of the same program will be (re)using the same application config (.exe.config - app.config in Visual Studio).
In our scenario we need to run each of the instances with their own .exe.config. This is not something that .Net is made to do "out of the box". However this nice little wrapper does the trick for us: https://stackoverflow.com/a/6151688/95008
This worked fine for us, until we realized that NLog does not seem to respect the app-config change. We are adding a config-section in the .exe.config for NLog which should make it possible each instance to log with different rules and targets to different locations.
But, it seems that NLog is not respecting the app-config changes. My guess is that it internally is not asking via the AppSettings API for its config-section, instead perhaps using code to manually look up the .exe.config and the config-section within it.
So, three alternatives it seems:
Look into NLog changing the code for this in a future release (given that this indeed is the issue). I will be posting an issue with the project for Update: Link to NLog issue.
Loading the configuration manually, either from the section of the .exe.config or from a specific nlog.config file (maybe given via a setting in the .exe.config). But, how do we do this effectively, so that any loggers created are based on the configs manually loaded?
Maybe there is another solution for this?
My guess is that it internally is not asking via the AppSettings API for its config-section, instead perhaps using code to manually look up the .exe.config and the config-section within it.
That's correct, as NLog is using the same code for the NLog's config, whether it's in web.config or in nlog.config
Reloading the config is easy in code, just do:
NLog.LogManager.Configuration = NLog.LogManager.Configuration.Reload();
and if there are loggers made before the config is reloaded,
NLog.LogManager.ReconfigExistingLoggers()
I have a wrapper class for log4net to log across multiple classes and assemblies within a group of dll-libraries. (This may be poor design, but.. don't ask)
The code basically looks like this:
public static class Logger
{
private static ILog log;
static Logger()
{
log = LogManager.GetLogger("abcd");
XmlConfigurator.Configure(new FileInfo("LoggerConfig.log4net"));
log.Info("=== NEW SESSION ===");
}
public static void Log(string message)
{
log.Debug(message);
}
}
Now.. if the static constructor was the static main routine of a simple executable, this works perfectly fine (I quickly made a console app to test it). But here it does not.
Since I deal with standalone assemblies, not executables, the only way I can easily test the logger is through MSTest. This may be a cause for this problem. I am not 100% sure if it maybe simply can't find the config file. It is copied at compilation and lies in the same folder as the assembly containing the logger class, so i think that should work.
bleh.. stuck since three hours. :T any suggestions?
My psychic debugger says that your LoggerConfig.log4net is not in the right directory. It is not deployed to your test directory and not found when running the test. That is why there is no log output. Deploy the file in the test directory and you will have you logging output.
EDIT: More precisely, you need to add it as a deployment file under Test->Edit Test Settings->>Deployment (as described here: How can I get "Copy to Output Directory" to work with Unit Tests? )
Have you tried to reference the config file by an absolute path? Maybe the root directory for MSTests is different to the Console App root dir?
Log4net uses configs in App.config? Try to put them also in the testlibrary?
I know the problem by Application with many libraries and there de config must only be in the Main-Application.
MsTest shadow copies things all over the place and as peer and Hinek say the location of the file is probably the issue. I find including files within the test assembly is one way to get around this.
Check out a previous answer here for details.
We are using log4Net to log messages into files and database from our .Net 3.5 windows application.
The winforms application is deployed on to production environment and is up and running.
Before the deployment, the level node attribute value is set to "ALL".
<level value="ALL"/>
While the application is running, I need to change the level to say "ERROR" and save the xml.And then, the log4net should log only "error" type messages in the log.
How do i achieve this using log4net?
If not feasible,any other approach please?
Thanks.
By default, if you keep your log4net configuration in a separate file (as opposed to in the app.config file) you can update that file and the application will immediately change what level it logs information thanks to the XmlConfigurator in log4net.
If you'd like to update the logging level dynamically from within your application, there's a simple way of doing that thanks to the LINQ to XML capabilities from .Net 3.5+. I've written a blog post outlining how to do that here.
Using
[assembly:
log4net.Config.XmlConfigurator(Watch=true)]
may work, according to the documentation
In the application you would need to have a filewatcher on the config file, then re-run the log4net initialisation on change of config file.