Background Application in AppHarbor and Logentries - backgroundworker

Now I've a web application and 3 background application in AppHarbor .
I want to use logentries to log each app in different location.
based on logentries docs for AppHarbor https://logentries.com/doc/appharbor/ the token keys are read from web.config and app.config [configuration vars] enforced by appharbor.
How can I configure each application to log in different log location (eg. Appharbor/Website, AppHarbor/Console1, AppHarbor/Console2)?

You can override the config vars injected by AppHarbor by specifying the Token in the appender/target definition. Not sure which framework you're using but if its NLog then
where you have this line in your web/app.config
<target name="logentries" type="Logentries" debug="true"
layout="${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}"/>
Add token="abc" so that you have this:
<target name="logentries" type="Logentries" debug="true" token="abc"
layout="${date:format=ddd MMM dd} ${time:format=HH:mm:ss} ${date:format=zzz yyyy} ${logger} : ${LEVEL}, ${message}"/>
And then if you're using the log4net plugin, where you have this section:
<appender name="LeAppender" type="log4net.Appender.LogentriesAppender, LogentriesLog4net">
<Debug value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{ddd MMM dd HH:mm:ss zzz yyyy} %logger %: %level%, %m" />
</layout>
</appender>
Add so that you have this:
<appender name="LeAppender" type="log4net.Appender.LogentriesAppender, LogentriesLog4net">
<Debug value="true" />
<Token value="abc" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{ddd MMM dd HH:mm:ss zzz yyyy} %logger %: %level%, %m" />
</layout>
</appender>
Token values set here will take precedence over those injected by Appharbor, so for each app that has its own web/app.config you can enter your own token using this method.

I Found that I can set the token in runtime by below lines :
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(Program));
appender = (LogentriesAppender)log.Logger.Repository.GetAppenders()[0];
appender.Token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
//now log method will use my token
log.Info("Hello World");

Related

Writing different levels to different files in log4cxx

I want to have log messages from each log level go to a different file. From the name, LevelMatchFilter seems like what I want, except it seems to not filter anything from a different level.
I think the following properties should do that using LevelRangeFilter. However, anything sent to the global logger ends up in INFO.log, regardless of the level.
log4j.rootLogger = OFF
# Global level based logs
log4j.logger.global = ALL, Info
log4j.appender.Info=org.apache.log4j.FileAppender
log4j.appender.Info.File=Logs/INFO.log
log4j.appender.Info.layout=org.apache.log4j.PatternLayout
log4j.appender.Info.layout.ConversionPattern=%d [%p] %m%n
log4j.appender.Info.filter.a=org.apache.log4j.filter.LevelRangeFilter
log4j.appender.Info.filter.a.LevelMin=info
log4j.appender.Info.filter.a.LevelMax=info
log4j.appender.Info.filter.a.AcceptOnMatch=true
I also tried using INFO for the values of LevelMin and LevelMax but that had the same results.
What am I doing wrong?
As a side question, is there a way to turn on debugging of the log4cxx configuration when using a property file? I found an option when using an xml file, but none of the obvious translations to properties (debug=true, log4j.debug=true) and any effect.
As of log4cxx 0.10 (and probably earlier), the properties format does not support filters. So the XML configuration (or programmatic configuration) is required.
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="Info" class="org.apache.log4j.FileAppender">
<param name="file" value="Logs/INFO.log" />
<param name="append" value="false" />
<!-- If this filter accepts the message, it will be printed. That happens if this is an info message -->
<filter class="org.apache.log4j.filter.LevelMatchFilter">
<param name="levelToMatch" value="INFO" />
<param name="acceptOnMatch" value="true" />
</filter>
<!-- If it is not an info message, this filter will reject it -->
<filter class="org.apache.log4j.filter.DenyAllFilter"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%p] %m%n" />
</layout>
</appender>
<root>
<priority value="off" />
</root>
<logger name="global">
<priority value="all" />
<appender-ref ref="Info" />
</logger>
</log4j:configuration>

Log4net in a web service

I basically have the same question as
log4net only works when XmlConfigurator.Configure() is called
However, I couldn't comment there as I don't have any reputation (just signed up).
Thanks for any helpful comments.
If I do anything wrong here, please advise.
Thank you very much. Bernd
Update:
Thanks for constructive hints. I've have made some progress and therefore will explain in more detail:
I use log4net within a (VS generated C# web service). I do get the debug information in the debug file, however within VS (2012) I do get the message for every logging call:
log4net:ERROR An exception ocurred while retreiving stack frame information.
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei log4net.Core.StackFrameItem..ctor(StackFrame frame)
I configured it via XML:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="Logs/debug.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level (%class#%method:%line):%message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
My web service looks like this:
public class ObjectInfo : IObjectInfo
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void DoWork() {
Logging.LoggingUtil.InitializeLogging();
log.Debug("Some message");
}
}
Hope this is quite sufficient. The LoggingUtil- class basically looks like this:
private const String Log4NetConfigurationFilePath = "log4net-config.xml"; //real path looks somewhat different
public static void InitializeLogging()
{
XmlConfigurator.Configure(new FileInfo(Log4NetConfigurationFilePath));
}
I wonder, if the problem is that the stack trace cannot be found out within Cassine as Microsoft doesn't allow this in order to protect their implementation of the web service?
As explained in LOG4NET-393, this error occurs when there is a dynamic method in the call chain.
There they claim to have this error fixed in versions > 1.2.12.

Log4net doesn't log online

I'm trying to use log4net to log errors on my MVC 4 website. It works fine offline, but when we publish the website, log4net doesn't even create the log file.
Here is the Web.config code:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="mylog.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<header value="**"/>
<footer value="**"/>
<conversionPattern value="%newline%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
I added this to the Assembly:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Here is my code to call the logger:
public void logInfo(string message)
{
log4net.Config.XmlConfigurator.Configure();
logger.Info(message);
}
This is bcoz,the root folder doesn't have write permissions where you are trying to create the log file.
steps to provide write permissions:
1) right click on "folder"
2) go to properties
3) click on security
4) Click on add-> add everyone-> check on full control.
it will work fine now.

Does the Log4net SMTPAppender send email Asynchronously?

Does the Log4net SMTPAppender send email asynchronously? If it doesn't, how can I send logging emails asynchronously?
My log4net.config is:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
<authentication value="Basic" />
<to value="xxx#xx.com" />
<from value="yyy#xx.com" />
<username value="yyy#xx.com" />
<password value="yyy" />
<subject value="xxx" />
<smtpHost value="smtp.xx.com" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN" />
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger %newline %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="INFO"></level>
</root>
<logger name="MyLogger">
<level value="INFO"></level>
<appender-ref ref="SMTPAppender"></appender-ref>
</logger>
</log4net>
</configuration>
You could just call the logging method asynchronously like this:
Task.Factory.StartNew(() => log.Info("Message I want to email"));
I actually got this suggestion from the following SO article:
How do I create an asynchronous wrapper for log4net?
There is a simple workaround before the solid solution is released.
public class SmtpAsyncAppender : SmtpAppender
{
protected override void SendEmail(string messageBody)
{
Task.Run(() => base.SendEmail(messageBody));
}
}
It presumes that SmtpAppender.SendEmail is thread-safe and reenterable. It is for log4net v1.2.13.0 and there is no reason not to be in the future.
Log4net has not built in appender that is asynchronous. If you need that functionality you need to write your own appender. Downloading the log4net source code should get you started...

Can't get Log4Net to work in my ASP.NET website :(

really simple question -> i can't seem to get any data from Log4Net in my ASP.NET application. I've got a simple ASP.NET website, which references a class library. In this class library, I have some lines that call the logger.
I'm trying to read the log4net output data in my Visual Studio 2008 debugging Output window.
Here's my code and my configuration...
//Class Library project
//File: Foo.cs
public class FooService
{
private static readonly ILog log = LogManager.GetLogger(typeof(FooService));
public FooService()
{
// NOTE: To play with my L4N settings, I'll call Debug once, then Info once.
log.Info("Starting Constructor");
// ... snip ...
log.Debug("Leaving Constructor");
}
}
// ASP.NET Website project
// File: global.asax
void Application_Start(object sender, EventArgs eventArgs)
{
log4net.Config.XmlConfigurator.Configure();
}
// File: Whatever.aspx.cs
// A delegate method (when a user clicks a button) creates the FooService() instance.
// File: web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />
// ....
</configSections>
<log4net>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Blue" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="App_Data\logging\log-append.txt"/>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="OutputDebugStringAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
<!-- Specify the level for some specific categories -->
<logger name="DotNetOpenAuth">
<level value="ALL" />
</logger>
</log4net>
Cheers for any help or suggestions...
EDIT: Added the RollingLogFileAppender.
I had this same problem and I think it was looking at the wrong web.config or something. I finally separated out log4net.config from web.config and put a path to it \inetpub\Logs\log4net.config and all is well.
UDPATED ON REQUEST: edited from a slightly more complicated version.
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level (%logger:%line) - %message%newline" />
</layout>
</appender>\
<root>
<!--ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF-->
<level value="ALL" />
<appender-ref ref="TraceAppender" />
</root>
</log4net>
And it is configured in code as follows:
var logpath = WebConfigurationManager.AppSettings["LogConfigPath"] ?? #"\Inetpub\Logs\log4net.config";
var finfo = new System.IO.FileInfo ( logpath );
XmlConfigurator.Configure( finfo );
ASP.Net has restriction on usage of filesystem access, so try to explicitly point directory under App_Data (were it is allowed)
Here my working sample:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="App_Data\logging\log-file.txt"/>
Or with rollover
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="App_Data\logging\log-append.txt"/>
Ok, found the answer. I needed to use a TraceAppender.
The application configuration file
can be used to control what listeners
are actually used. See the MSDN
documentation for the Trace class for
details on configuring the trace
system.
Events are written using the
System.Diagnostics.Trace.Write(string,string)
method. The event's logger name is
passed as the value for the category
name to the Write method.
Here's my config file data...
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="TraceAppender" />
</root>
</log4net>
There are atleast 2 possible problems:
The way the address to the file is specified, try using an absolute path instead.
The process making the call needs rights to modify the file. Check that the user account making writing to the log file has rights to do so.
To debug it I would create and empty directory, give everyone full control, configure to log to that directory. Then test it, see that it works, then gradually tighten the security to an acceptable level.
I added the following line Global.asax file, and it works..!!
log4net.Config.XmlConfigurator.Configure();

Resources