I want to create a log text file for my mobile application.I am working on compact framework 3.5 and i chosen log4net for logging.I followed the below blog to create the Config.xml file and log file http://breathingtech.com/2009/using-apache-log4net-in-net-compact-framework-projects/ which runs without any error but not created any log file.See my Config.xml file,
<?xml version="1.0" encoding="utf-8" ?>
<!-- .NET application configuration file -->
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders
and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
I added the below line under program.cs file
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Program));
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\Config.xml";
if (System.IO.File.Exists(path))
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));
}
Log.Info("Application startup");
Do my Config.xml is correct,i just copied the content from the above blog and i am getting the Config.xml path in program.cs.
Since the above setup not created log-file.txt and not logged the log.Info message,i googled and added the below line after the using log4net.Config;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "../Config.xml", Watch = true)]
This give me an error "'log4net.Config.XmlConfigurator' is not an attribute class". Can any one help me in creating a log file in compact framework.
Thanks
Try using following log4net configurations. I use this and it generates log file.
<log4net>
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log.txt"/>
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="yyyy.MM.dd"/>
<param name="StaticLogFileName" value="true"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %-5p - %m%n"/>
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="ALL"/>
<appender-ref ref="rollingFile"/>
</root>
</log4net>
In your log4net config file you do not need to add the <configuration> root tag. <log4net> should be the root. The <configuration> is the root tag if you are putting your config in the application config file or web config file.
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!-- Define some output appenders -->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders
and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
Related
I have created a Console App (.Net Framework) project for my NSelene UI automation project. Currently, I am configuring log4net. Unfortunately, I am unable to log into an output window (log4net is writing only into a file).
Test output showing next error:
log4net:ERROR XmlHierarchyConfigurator: No appender named [DebugAppender] could be found.
log4net:ERROR Appender named [DebugAppender] not found.
I have added [assembly: log4net.Config.XmlConfigurator(Watch = true)] into AssemblyInfo.cs file
app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="DebugAppender "
type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -
%m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="log/SBT.UI.log"/>
<encoding value="utf-8"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss},
%message%newline"/>
</layout>
</appender>
<appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -
%m%n" />
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="DebugAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Project.cs
[SetUp]
public void Initialize()
{
logger.Debug("test1");
logger.Info("test2");
logger.Warn("test3");
logger.Error("test4");
logger.Fatal("test5");
}
I am using log4net for logging in my webapi. logging is working fine in my localhost or while i am deploying it on a app server. Same thing is not working in azure web service. Folder where log file has to be created is there but no files in there.
i have added below section in my web.config file,
<log4net>
<appender name ="ErrorLog" type ="log4net.Appender.RollingFileAppender">
<file value ="MyLogs\"/>
<staticLogFileName value ="false" />
<appendToFile value ="true"/>
<rollingStyle value ="Date"/>
<datePattern value ="yyyy-MM-dd.'Err'" />
<lockingModel type ="log4net.Appender.FileAppender+MinimalLock"/>
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value ="%d{DATE} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name ="ErrorLog">
<maximumFileSize value ="15MB" />
<appender-ref ref="ErrorLog"/>
</logger>
</log4net>
It seems that there are some mistake with log4net configuration.
Here I have a complete steps you could follow with and it works well on my site:
1.Install the log4net.dll binary using NuGet.
2.Configure the log4net name, type properties in the web.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
</configuration>
3.Configure the log4net properties
<log4net>
<root>
<level value="Debug"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\Home\LogFiles\Log4Net\Log4Net.log"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="3MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<logger name="SleepyCore">
<level value="INFO"/>
</logger>
</log4net>
Note: You need to specify the address of the log on azure like D:\Home
4.Modify the Global.asax Application_Start() method add the following code in the method.
log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
5.Write log with an instance of the ILog interface
ILog log = LogManager.GetLogger("SleepyCore");
log.Info("Begin - Page_Load() at " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
6.The output as below:
For more details about how to configure log4net on azure, you could refer to this article.
I am using the following code but it never creates a new hourly based log file.
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="C:\ATSApiLog\APILog_%date{yyyyMMdd-HH}.log" />
</file>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss,fff}, %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
I have a windows service application which is basically a solution created with VS2015 and contains 2 projects.
The first project is dealing with the service itself (start, stop, etc) while the second one is maintaining the process that will be executed due to launch of the service.
I am using the log4net to log messages into a file.
I wanted to created 2 separate log files for each project.
This has been configured into the service project and is up and running.
while it is not working for the second project.
So mainly my question is how to configure log4net to log into different files within one solution?
Configure log4net within the executing project only. If you want log messages from an specific assembly logged to a separate file make sure the root namespace for that assembly is unique and define two log4net appenders, each logging to a separate file and use logging filters to filter by namespace.
<!-- Will log only from the My.Process namespace -->
<appender name="ProcessLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="process.log" />
<!-- layout, etc -->
</appender>
<!-- will log everything except from the My.Process namespace -->
<appender name="ServiceLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="false" />
</filter>
<file value="service.log" />
<!-- layout, etc -->
</appender>
And don't forget to add a reference to both appenders in the root element.
Assuming your first and second project have different root namespaces (e.g. Project1.Service and Project2.MaintainProcess), you can use 2 appenders and the logger hierarchy to split the log files by project:
<log4net>
<appender name="Project1Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project1Logger.log" />
</appender>
<appender name="Project2Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project2Logger.log" />
</appender>
<root>
<level value="ALL" />
</root>
<logger name="{Project1RootNamespaceHere}">
<appender-ref ref="Project1Logger" />
</logger>
<logger name="{Project2RootNamespaceHere}">
<appender-ref ref="Project2Logger" />
</logger>
</log4net>
If you don't want to use separate appenders and logger hierarchy then see this answer for an alternative method using a custom pattern substitution to use a single appender. I'm not sure of the performance implications of that approach though.
Complete configuration (with a MinimalLock file appender):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<log4net>
<!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
<file value="Info.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
<file value="Error.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender_Info" />
<appender-ref ref="RollingFileAppender_Error" />
</root>
</log4net>
</configuration>
I have some NServiceBus endpoints hosted as Windows Services and a few MVC 5 web sites in the same solution. All the projects use the same Assembly configuration for log4net and the same config file (literally the same files, they are shared files). The desired output is the UDP appender output to a Log2Console session.
The windows services log everything fine, but the MVC web sites do not.
Here is the assembly config:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4netconfig.xml", Watch = true)]
And here is the content of "log4netconfig.xml":
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="9998" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="UdpAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Edit
After removing the assembly config attribute, adding a log statement as the first item in Application_Start and setting up the below in web.config, I still get no logging:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="9998" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="UdpAppender" />
</root>
</log4net>