Programmatically set Nhibernate to lower logging level for log4net - c#-4.0

I have an application which logs to log4net but also uses Nhibernate. My app configures both Nhibernate (using Fluent Nhibernate config) and log4Net (using BasicConfigurator) programmatically .
Problem is my logs are full of Nhibernate log info which I don't need 99.9% of the time and the app slows down due to the full logging from Nhibernate.
How can I configure Nihbernate to not do any logging or log4Net to ignore all Nhibernate loggers programmatically? I know you can do it using xml config files but this is not an option for me.
Any help would be much appreciated.

See Log4Net: Programmatically specify multiple loggers (with multiple file appenders) where I stole this from:
public static void SetLevel(string loggerName, string levelName)
{
ILog log = LogManager.GetLogger(loggerName);
Logger l = (Logger)log.Logger;
l.Level = l.Hierarchy.LevelMap[levelName];
}
SetLevel("NHibernate","Error");

Related

Logging with Nlog & Rebus

I would like to implement ReBus logging with Nlog.
We have NLog running to console, file and Database (with custom fields) and different files. I mean, NLog is up & running fine.
We have installed ReBus.Nlog Nuget Package and changed our adapter configuration to Nlog().
But nothing is logged to files in spite of we have it configured with 'Trace' loglevel.
Somebody has any simple example to implement loggin with NLog in ReBus?
I have checked the tests on Rebus.Nlog github source code, but I think it is only testing explicit sent messages to logger.
Mainly I would like to log Rebus warnings and errors.
Thanks.
I have added this Logging sample project to the RebusSamples repository – the relevant part of the code is shown here:
// configure NLog
var configuration = new LoggingConfiguration
{
LoggingRules = { new LoggingRule("*", LogLevel.Debug, new ConsoleTarget("console")) }
};
LogManager.Configuration = configuration;
// configure Rebus
Configure.With(activator)
.Logging(l => l.NLog())
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "logging"))
.Start();
As far as I can tell, this way of configuring NLog may not be the idiomatic way, since I guess most people prefer to be able to configure rules and targets with XML in their application configuration files.
Either way, NLog ends up having the static configuration applied, and then Rebus can pick that up when you call NLog() on its logging configurer.
I hope that helps :)

ServiceStack Log4NetFactory

How can I configure log4net in code when I like to use the servicestack logging interface? I see there is
LogManager.LogFactory = new Log4NetFactory(configureLog4Net:true);
If I got things right this expects a XML file containing the Log4Net configuration. I avoid XML config files whenever possible and usually configure Log4Net with my own static LoggerConfig class. There I setup all my appenders (EventLog, File, Console, etc.) using the Log4Net API.
Is there any way to integrate my class with the ServiceStack logging interface?

how to configure log4net to work inside a CRM 2011 plugin?

I am trying to log some information inside a CRM 2011 plugin. I am not sure how to configure log4net. Where should I put log4net config file and how to reference from the plugin? Thanks!
Assuming you are registering your plugins to the database, you have a couple options:
Configure log4net programmatically. This can be done via the log4net API and could be driven by a configuration entity in crm.
Embed the log4net config file in the plugin assembly and configure log4net from a stream (shown below in a plugin base class that other plugins who wish to log could inherit from)
namespace TestPlugins
{
public abstract class BaseLoggingPlugin
{
protected static readonly ILog _log = LogManager.GetLogger(typeof(BaseLoggingPlugin));
static BaseLoggingPlugin()
{
using(var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestPlugins.log4net.config"))
{
XmlConfigurator.Configure(config);
}
}
}
}
I want to add a warning to whatever the correct answer ends up being:
if you are registering your plugin assembly as a sandbox assembly (sandbox mode is required for CRM-online) you will not have access to the file system. In that case, your only option is Tracing. Good luck!

Can multiple log4j.properties files be used in the same Tomcat web app?

I'm writing a custom extension to an off-the-shelf Java web application. The application uses log4j for logging and I'd like to add a new logger and appender specifically for my extension. The problem is that the application manages the log4j.properties file which is dynamically generated based on selections in an admin screen UI. Since this is an "off-the-shelf" application, I can't modify the source code. So, if I add my own logger & appender(s) to the file, it gets overwritten anytime an admin changes logging preferences in the UI.
Is it possible to get log4j to get it's configuration from 2 files? For example, I'd want something like the following:
applog.properties #(Dynamically generated from admin UI)
mylog.properties #(My static properties)
In this scenario, log4j would somehow combine the entries from both files for the complete configuration.
Is this possible? or are there other workarounds?
I never did find a way to "merge" multiple log4j.properties file, but I did find a workable solution. log4j configuration can be manipulated programatically at runtime similar to the code snippet below. This effectively merged my custom log4j settings into the configuration defined by the log4j.properties file, which in my case I couldn't edit.
// Init custom logging
// Define layout
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d [%-5p] -- %m%n");
// Create appender
RollingFileAppender appender = new RollingFileAppender();
appender.setFile(LOG_PATH);
appender.setMaxFileSize("2MB");
appender.setMaxBackupIndex(0);
appender.setLayout(layout);
appender.activateOptions(); // It didn't work without this
// Get our logger and add appender.
log = Logger.getLogger("[MyCustomLogger]");
log.setLevel(YOUR_LOGGING_LEVEL_HERE);
log.addAppender(appender);

how to change the logging level of logger in the code overriding value set on config

I have a C# Win Form application and in each class i get the logger like this:
private static readonly ILog Log = LogManager.GetLogger("ApplnLogger");
I configured in my app.config to log messages at info level and above.
Now when a issue is reported i would like to set the logger level to debug so that i will have information in log file necessary for debugging the issue.
How do I set the logger level to debug overriding the config level of INFOwithout having to restart my win form application?
Check out ConfigureAndWatch
Basically, you tell your program to watch the configuration file for changes. So, if you edit this file, Log4net will automatically use the new settings without you having to restart your application.

Resources