ServiceStack Log4NetFactory - servicestack

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?

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 :)

Logging from within WSO2 custom mediator

I want to be able to log from within a custom mediator that I have built.
A few questions:
What do I need to add to the esb's log4.properties to enable a custom class the ability to write to log files?
From within the custom mediator class, do I need declare the following to log to the synapse log file?
private static final Log log = LogFactory.getLog(<ClassName>.class);
I have seen many examples of using the log mediator, but I need to be able to control more of what I log from within mediator class.
By default your custom mediator logs will be sent to Carbon Log file as well as Carbon Memory and the console. And also you do not need to define
private static final Log log = LogFactory.getLog(.class);
again in your class, you can simply use the log object which is coming from AbstractMediator
Please refer the following guide which explains how to write your custom mediator as well as how you should log inside a mediator.
You can change the level of the log by editing the log4j.properties file or by going to configure -> logging using the management console to get more control on what to log and what not to log.
You can add the mediator class to log4j.properties
log4j.logger.org.foo.bar=ERROR, CARBON_LOGFILE, CARBON_MEMORY
Regards,
/Nuwan

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!

Programmatically set Nhibernate to lower logging level for log4net

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");

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);

Resources