470,811 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

log4net in console app

I am using log4net in a vb.net console application. But its not working. I
have added congif setting in app.config file also
No error is generated but app. does not write to log file.
app.config file:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<!-- Register the section handler for the log4net section -->

<configSections>

<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler,log4net" />

</configSections>

<appSettings>
</appSettings>

<!-- This section contains the log4net configuration settings -->

<log4net debug="true" >

<!-- Define some output appenders -->
<!-- Rolling file appender -->

<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net ">

<param name="File" value="Dataqueueworker-Errlog.txt" />

<param name="AppendToFile" value="true" />

<param name="MaxSizeRollBackups" value="10" />

<param name="MaximumFileSize" value="1000KB" />

<param name="StaticLogFileName" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">

<param name="Header" value="[Header]\r\n"/>

<param name="Footer" value="[Footer]\r\n"/>

<!-- <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"
/> -->

<!-- <param name="ConversionPattern" value="%-5p %d [%t] %m%n" /> -->

<param name="ConversionPattern" value="%-5p %d %m%n" />

</layout>

</appender>

<!-- File appender -->

<appender name="LogFileAppender"
type="log4net.Appender.FileAppender,log4net" >

<param name="File" value="DAtaqueueworker-log.txt" />

<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%-5p %d [%t] %m%n" />

<!--

<param name="ConversionPattern"

value="%d [%t] %-5p %c[%x] &lt;%X{auth}&gt; - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

-->

</layout>

</appender>
<!-- ConsoleAppender -->

<appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender,log4net" >

<layout type="log4net.Layout.PatternLayout,log4net">

<!--

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
&lt;%X{auth}&gt; - %m%n" />

-->

<param name="ConversionPattern"

value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- ADONetAppender -->

<appender name="ADONetAppender"
type="log4net.Appender.ADONetAppender,log4net" >

<param name="ConnectionString"

value="Server=localhost;Initial Catalog=TesTest;user id=sa;password=;Connect
Timeout=5" />

<layout type="log4net.Layout.PatternLayout,log4net">

<!-- <param name="ConversionPattern" value="INSERT INTO Log (Created,
Thread, Priority, Logger, Message, Context) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%p','%c','%m','%x')" /> -->

<param name="ConversionPattern"

value="INSERT INTO Log (created, thread, result, principal, resource,
entitlement, arguments, message) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- Setup the root category, add the appenders and set the default
priority -->

<root>

<priority value="INFO" />

<appender-ref ref="RollingLogFileAppender" />

<appender-ref ref="LogFileAppender" />

</root>

<!-- Specify the priority for some specific categories -->

<category name="TesPrivilegeLog">

<priority value="INFO" />

<appender-ref ref="ADONetAppender" />

</category>
</log4net>


</configuration>


Apr 10 '06 #1
2 4933
Hi Vikram,

following is the contents of my App.Config -

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler, log4net" />
</configSections>
<log4net debug="false">
<appender name="LogFileAppender"
type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="c:\\Log4NetConsole.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> -
%m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<category name="Test"><priority value="ALL" /></category>
</log4net>
</configuration>

And my class file contents are -

using System;
using log4net;

namespace Log4NetConsoleApp
{

class Class1
{
private ILog logger = LogManager.GetLogger("Test");

[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure();
Class1 obj = new Class1();
obj.log("Testing");

}

private void log(string str)
{
logger.Info(str);
}
}
}

This works for me.

"Vikram" wrote:
I am using log4net in a vb.net console application. But its not working. I
have added congif setting in app.config file also
No error is generated but app. does not write to log file.
app.config file:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<!-- Register the section handler for the log4net section -->

<configSections>

<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler,log4net" />

</configSections>

<appSettings>
</appSettings>

<!-- This section contains the log4net configuration settings -->

<log4net debug="true" >

<!-- Define some output appenders -->
<!-- Rolling file appender -->

<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net ">

<param name="File" value="Dataqueueworker-Errlog.txt" />

<param name="AppendToFile" value="true" />

<param name="MaxSizeRollBackups" value="10" />

<param name="MaximumFileSize" value="1000KB" />

<param name="StaticLogFileName" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">

<param name="Header" value="[Header]\r\n"/>

<param name="Footer" value="[Footer]\r\n"/>

<!-- <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"
/> -->

<!-- <param name="ConversionPattern" value="%-5p %d [%t] %m%n" /> -->

<param name="ConversionPattern" value="%-5p %d %m%n" />

</layout>

</appender>

<!-- File appender -->

<appender name="LogFileAppender"
type="log4net.Appender.FileAppender,log4net" >

<param name="File" value="DAtaqueueworker-log.txt" />

<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%-5p %d [%t] %m%n" />

<!--

<param name="ConversionPattern"

value="%d [%t] %-5p %c[%x] <%X{auth}> - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

-->

</layout>

</appender>
<!-- ConsoleAppender -->

<appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender,log4net" >

<layout type="log4net.Layout.PatternLayout,log4net">

<!--

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
<%X{auth}> - %m%n" />

-->

<param name="ConversionPattern"

value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- ADONetAppender -->

<appender name="ADONetAppender"
type="log4net.Appender.ADONetAppender,log4net" >

<param name="ConnectionString"

value="Server=localhost;Initial Catalog=TesTest;user id=sa;password=;Connect
Timeout=5" />

<layout type="log4net.Layout.PatternLayout,log4net">

<!-- <param name="ConversionPattern" value="INSERT INTO Log (Created,
Thread, Priority, Logger, Message, Context) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%p','%c','%m','%x')" /> -->

<param name="ConversionPattern"

value="INSERT INTO Log (created, thread, result, principal, resource,
entitlement, arguments, message) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement}
','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- Setup the root category, add the appenders and set the default
priority -->

<root>

<priority value="INFO" />

<appender-ref ref="RollingLogFileAppender" />

<appender-ref ref="LogFileAppender" />

</root>

<!-- Specify the priority for some specific categories -->

<category name="TesPrivilegeLog">

<priority value="INFO" />

<appender-ref ref="ADONetAppender" />

</category>
</log4net>


</configuration>


Apr 10 '06 #2
Thanks Kaustav.
I was missing "log4net.Config.DOMConfigurator.Configure(); "
statement.

"Kaustav" <Ka*****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi Vikram,

following is the contents of my App.Config -

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler, log4net" />
</configSections>
<log4net debug="false">
<appender name="LogFileAppender"
type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="c:\\Log4NetConsole.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> -
%m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<category name="Test"><priority value="ALL" /></category>
</log4net>
</configuration>

And my class file contents are -

using System;
using log4net;

namespace Log4NetConsoleApp
{

class Class1
{
private ILog logger = LogManager.GetLogger("Test");

[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure();
Class1 obj = new Class1();
obj.log("Testing");

}

private void log(string str)
{
logger.Info(str);
}
}
}

This works for me.

"Vikram" wrote:
I am using log4net in a vb.net console application. But its not working. I have added congif setting in app.config file also
No error is generated but app. does not write to log file.
app.config file:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<!-- Register the section handler for the log4net section -->

<configSections>

<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler,log4net" />

</configSections>

<appSettings>
</appSettings>

<!-- This section contains the log4net configuration settings -->

<log4net debug="true" >

<!-- Define some output appenders -->
<!-- Rolling file appender -->

<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net ">

<param name="File" value="Dataqueueworker-Errlog.txt" />

<param name="AppendToFile" value="true" />

<param name="MaxSizeRollBackups" value="10" />

<param name="MaximumFileSize" value="1000KB" />

<param name="StaticLogFileName" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">

<param name="Header" value="[Header]\r\n"/>

<param name="Footer" value="[Footer]\r\n"/>

<!-- <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"
/> -->

<!-- <param name="ConversionPattern" value="%-5p %d [%t] %m%n" /> -->

<param name="ConversionPattern" value="%-5p %d %m%n" />

</layout>

</appender>

<!-- File appender -->

<appender name="LogFileAppender"
type="log4net.Appender.FileAppender,log4net" >

<param name="File" value="DAtaqueueworker-log.txt" />

<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%-5p %d [%t] %m%n" />

<!--

<param name="ConversionPattern"

value="%d [%t] %-5p %c[%x] <%X{auth}> - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement} ','%X{arguments}','%X{message}')" />

-->

</layout>

</appender>
<!-- ConsoleAppender -->

<appender name="ConsoleAppender"
type="log4net.Appender.ConsoleAppender,log4net" >

<layout type="log4net.Layout.PatternLayout,log4net">

<!--

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]
<%X{auth}> - %m%n" />

-->

<param name="ConversionPattern"

value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement} ','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- ADONetAppender -->

<appender name="ADONetAppender"
type="log4net.Appender.ADONetAppender,log4net" >

<param name="ConnectionString"

value="Server=localhost;Initial Catalog=TesTest;user id=sa;password=;Connect Timeout=5" />

<layout type="log4net.Layout.PatternLayout,log4net">

<!-- <param name="ConversionPattern" value="INSERT INTO Log (Created,
Thread, Priority, Logger, Message, Context) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%p','%c','%m','%x')" /> -->

<param name="ConversionPattern"

value="INSERT INTO Log (created, thread, result, principal, resource,
entitlement, arguments, message) VALUES ('%d{dd-MMM-yyyy
HH:mm:ss}','%t','%X{result}','%X{principal}','%X{r esource}','%X{entitlement} ','%X{arguments}','%X{message}')" />

</layout>

</appender>

<!-- Setup the root category, add the appenders and set the default
priority -->

<root>

<priority value="INFO" />

<appender-ref ref="RollingLogFileAppender" />

<appender-ref ref="LogFileAppender" />

</root>

<!-- Specify the priority for some specific categories -->

<category name="TesPrivilegeLog">

<priority value="INFO" />

<appender-ref ref="ADONetAppender" />

</category>
</log4net>


</configuration>


Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by SK | last post: by
reply views Thread by anonieko | last post: by
reply views Thread by Joneleth | last post: by
4 posts views Thread by Gawel | last post: by
2 posts views Thread by Marc Scheuner | last post: by
2 posts views Thread by forest demon | last post: by
4 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.