Hi there i'm trying to write log for my live website and am having problems with my paths, I need to use the method Server.MapPath but i need it done in my web.config.
How would i go about this?
The file value holds the path, but i need to set a Server.MapPath on it.
<log4net>
<add key="LogFilePath1" value="../Logs/CurrentLog" id="FP1"/>
<add key="LogFilePath2" value="../Logs/CurrentLog.txt" id="FP2" />
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="../Logs/CurrentLog"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10000"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true"/>
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n"/>
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="../Logs/Log.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="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger name="ConsoleApp.LoggingExample">
<level value="ERROR"/>
<appender-ref ref="EventLogAppender"/>
</logger>
Logger code C#:
public class Logger
{
protected static readonly ILog logger = LogManager.GetLogger(typeof(Logger));
public void logError(string message, Exception ex)
{
HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath1"]));
HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath2"]));
log4net.Config.XmlConfigurator.Configure();
logger.Error(message + ex);
}
public void logInfo(string message)
{
HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath1"]));
HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["LogFilePath2"]));
log4net.Config.XmlConfigurator.Configure();
logger.Info(message);
}
}
I have added C# code to Server.MapPath the Keys, i'm just struggling calling the keys in thhe file value =
Store your path in the config as
<add key="LogFilePath1" value="~/Logs/CurrentLog" id="FP1"/>
From your code read the key
string path = GetPath("LogFilePath1");
string fullPath = System.Web.HttpContext.Server.MapPath(path);
Log4net does not use Server.Map path when reading the paths from your configuration files. It does not know it is running in a web context. I geuss you can implement your own version of the Rolling File Appender and override the function which gets the path from the config. However I would just use the full paths to the log locations. Normally you do want to log on an other disk your application is running to prevent running out of diskspace and killing your application.
Related
We've been using Database appender (AdoNetAppender) in my company for a while. We're working on a deploy right now and not seeing any errors being persisted to the database. one thought we have is that the Database Connection is being blocked by a firewall, but you can already see where this goes into a circle of pain.
I added a RollingFileAppender, a FileAppender, tried using a bufferingForwarder, and have tried everything on stackoverflow's green world to solve this problem and absolutely nothing is changing. I even removed the reference to the AdoNetAppender and now it's just not logging errors at all, whereas with the AdoNetAppender it was logging errors twice -- once for AdoNetAppender, once for FileAppender.
My xml code is:
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="AdoNetAppender"/>
</root>
<appender name="AdoNetAppender">
<!--working xml. No need to post this information-->
</appender>
<appender name="BufferingForwarder" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="5" />
<lossy value="false" />
<appender-ref ref="fileAppender" />
<!-- or any additional appenders or other forwarders -->
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\log4net.log"/>
<datePattern value="yyyy-MM-dd'-FULL.log'" />
<appendToFile value="true"/>
<preserveLogFileNameExtension value="true"/>
<rollingStyle value="Size"/>
<maximumFileSize value="250KB"/>
<maxSizeRollBackups value="-1"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
My c# code is:
(using Logger: ExceptionFilterAttribute)
private static ILog Log;
private static ILog FileLog;
private static bool _isConfigured = false;
private static void ConfigureLogger()
{
if (_isConfigured) return;
XmlConfigurator.Configure();
FileLog = LogManager.GetLogger("RollingFileAppender")
Log = LogManager.GetLogger("AdoNetAppender")
_isConfigured = true;
}
public static void Error(string message, Exception e) {
ConfigureLogger();
Log.Error(message, e);
}
public override void OnException(HttpActionExecutedContext context)
{
Error("Unexpected error occurred", context.Exception);
ConfigureLogger();
if (FileLog != null)
FileLog.Error("Unexpected Error Occurred", context.Exception);
}
I have a C# application. I would like to integrate a log File. Then I try do this in App.config
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<appender name="InfoFileAppender" type="log4net.Appender.FileAppender">
<file value="info_logfile.txt" />
<appendToFile value="true" />
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
<file value="error_logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<level value="INFO" />
<appender-ref ref="InfoFileAppender" />
<level value="ERROR" />
<appender-ref ref="ErrorFileAppender" />
</root>
</log4net>
So I try to write in infoFile:
log.Info("chiamata json");
but I don't see any text in info_logfile.txt
Where is my error?
Can we help me?
Reguards
Do you instantiate your log file? Try the following to see that it works with a console appender. (this example is copied from David Saltner)
using log4net;
using log4net.Config;
public class LogTest
{
private static readonly ILog logger =
LogManager.GetLogger(typeof(LogTest));
static void Main(string[] args)
{
BasicConfigurator.Configure();
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
}
}
.
private static readonly ILog logger =
LogManager.GetLogger(typeof(LogTest));
This creates a logger for the class LogTest. You don't have to use a different logger for each class you have, you can use different loggers for different sections or packages within your code. The class of the logger is output to the log so you know where any logged information has come from.
BasicConfigurator.Configure();
This method initializes the log4net system to use a simple Console appender. Using this allows us to quickly see how log4net works without having to set up different appenders.
This should get you started. Check out this great article for a brief introduction on how to set up appenders and more.
If the configuration is in your app.config file you need to add the sections in the config file:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0,
Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>
It is better practice to configure your log4.net is a log4net.config file. Then you need to add the following line to your assembly which reads your configuration file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I am trying to set up logging using log4net, with no prior experience of log4net, in a wpf application
taken from this SO answer, I have
public partial class App : Application
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override void OnStartup(StartupEventArgs e)
{
XmlConfigurator.Configure();
base.OnStartup(e);
//Set data directory
string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + #"\BlowTrial";
if (!Directory.Exists(baseDir))
{
Directory.CreateDirectory(baseDir);
}
//Set logging
foreach (var appender in LogManager.GetRepository().GetAppenders())
{
var fileAppender = appender as FileAppender;
if (fileAppender != null)
{
fileAppender.File = fileAppender.File.Replace("|DataDirectory|", baseDir);
...
but there are no elements in LogManager.GetRepository().GetAppenders() despite the app.config having the following within the configuration node:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="|DataDirectory|\log.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
</log4net>
I was wondering why the appender "LogFileAppender" is not an element within the LogManager.GetRepository().GetAppenders() method, and how I might change the name of the file?
Thanks for your expertise
The RollingFileAppender does some stuff internally when it's initialized (using XmlConfigurator.Configure();), which results in the path of |DataDirectory|\log.txt throwing a System.ArgumentException when trying to retrieve the path. You can view how to debug log4net if you want more details here.
According to this answer, since you're using a special folder path anyways, you can specify this in the app.config, and not worry about setting the path from the code:
<param name="File" type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\BlowTrial\\log.txt"/>
Currently my application (C# Console App) uses file to initialize log4net, is there a way to initialize the log4net configurations from the database ?
I can either save my current file as XML in DB (SQL) but I am not sure how to initialize log4net from that within my application.
EDIT :
Current Implementation:
_logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
var file = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["log4net.Config"]);
log4net.Config.XmlConfigurator.Configure(file);
AppSettings has a location of the physical file which has the following:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="c:\%property{LogName}" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<datePattern value="MM-dd-yyyy'.log'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level – %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
There's an overload to XmlConfigurator.Configure that takes a stream as the parameter - so you just read your config from the database, turn it into a stream, and pass it to that method:
string config = GetConfigFromDb();
using (var MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(config))
{
log4net.Config.XmlConfigurator.Configure(ms);
}
Two instances of the same program are configured to log in separate files, here is my log4net.config:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log/svc-%property{ServiceName}"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="'-'yyyy'-'MM'-'dd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
And here's how I instance the logs:
log = LogManager.GetLogger(ServiceName);
GlobalContext.Properties["ServiceName"] = ServiceName;
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
Now, the services are called stuff like "Service1" "Service10" "Service2".
When I start services 1 and 10, the ones I'm testing with, they both create their log files:
svc-BettweenSvc1-2011-07-26.log
svc-BettweenSvc10-2011-07-26.log
But when the second service is started, the logs from the first one start being appended to the second log, and that one is no longer used.
How can I log to separate log files properly in windows services using log4net?
I believe the problem is that you only have one log4net Repository configured (most likely the default one). When you change the log4net log file name, therefore, all loggers start logging to the new file name.
I believe that you will need to configure separate repositories for each separate log file you want active at the same time.
I'm sorry I can't help more, I'm struggling with almost the same issue right now (among other, more pressing, issues).
Good luck!
edit:
check out this question/answer for a possible solution:
how-to-stop-log4net-from-writing-to-two-separate-files
For the record, this works, but it's not what I'm testing against, it still doesn't work in my service, I'll just post it to hopefully help others though:
namespace log4netMultiLogTester
{
public static class Program
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
Process.Start(new ProcessStartInfo("log4netMultiLogTester.exe", "testApp1"));
Process.Start(new ProcessStartInfo("log4netMultiLogTester.exe", "testApp2"));
}
else
{
var logId = args[0];
var log = LogManager.GetLogger(logId);
GlobalContext.Properties["ServiceName"] = logId;
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
log.Info("Starting log: {0}".FormatWith(logId));
do
{
log.Info("Log: {0}, latest entry is {1}".FormatWith(logId,DateTime.Now.Ticks));
} while (Console.ReadKey().Key != ConsoleKey.R);
}
}
}
public static class StringExtensions
{
public static string FormatWith(this string text,params object[] args)
{
return string.Format(text, args);
}
}
}
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log/svc-%property{ServiceName}"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="'-'yyyy'-'MM'-'dd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>