Why my dotnetOpenAuth fails using log4net? - c#

I'm trying to debug why I get this error:
Error occurred while sending a direct message or getting the response.
on this line:
consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(authCallbakUrl, null, null));
So I added log4net, but it doesn't work for me.
My web config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- Others sections -->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />
</configSections>
<!-- log4net is a 3rd party (free) logger library that dotnetopenid will use if present but does not require. -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="RelyingParty.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TracePageAppender" type="OpenIdRelyingPartyWebForms.Code.TracePageAppender, OpenIdRelyingPartyWebForms">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (GMT%date{%z}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="TracePageAppender" />
</root>
<!-- Specify the level for some specific categories -->
<logger name="DotNetOpenAuth">
<level value="ALL" />
</logger>
</log4net>
Tried with and without this:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" />
<bindingRedirect newVersion="4.0.30319" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I configure the log4net instance
log4net.Config.XmlConfigurator.Configure();

Your binding redirect suggests you're using log4net v4.0.30319, which doesn't exist. The latest version is 1.2.11, and the version DNOA is compiled against is 1.2.10. Once you fix the binding redirect I suspect it will work.

GlobalApplication.cs:
decorate it
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
To get a reference to logger from other places ex.:
ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Related

log4net not creating output file

I am new to logging and trying out with different appenders. I am trying to output the logs to a log file at a specific location. Though code is compiling and running fine. It is also showing Console logs but it is not adding those logs to output file. Here's my code:
Console file:
namespace LoggerTest
{
class Program
{
private static readonly ILog log = LogManager.GetLogger("General");
static void Main(string[] args)
{
GlobalContext.Properties["logFilePath"] = #"C:\Users\Z004N84C\Desktop\Output.log";
XmlConfigurator.Configure();
log.Info("This is info log");
}
}
}
and here's my app.config file:
<?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.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger %method [%line] - %message%newline" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="%property{logFilePath}/ToolLog.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
In above file I have added log4net configs.
I am not sure what I am missing.
I tried various configs but not able to figure this out. Need to create output file.

Log4net doesn't show exact error message

I'm using log4net as a global exception handler in my .NET Core 2.0 app.
I need to show exact error message and stacktrace. But at the moment it just say "An unhandled exception has occurred while executing the request"
This is my log4net configuration:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="logger.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<IgnoresException value="False" />
<conversionPattern value="%date %logger [%thread] [%ndc] (%file:%line) %level- %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>
How I can show exact error message and stacktrace?

log4net log file is not getting created in C#

log file is not getting created,
using below call to get logger and config file.
is it required to give full path for file?
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Does your code call
log4net.Config.XmlConfigurator.Configure();
on startup?
The <root> section I use has priority, not level:
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
(Apparently, that's not the issue https://stackoverflow.com/a/24188507/21336)
after adding below line in AssemblyInfo.cs, could see log file getting created and logs coming.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

How to configure log4net to log into multiple files

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>

log4net creates log file but does not write to it

I am trying to use basic logging for a windows service.
I added the reference to log4net.
I added the following in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I added the following to my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false" />
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<file value="c:\\CGSD\\log\\logfile.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
</configuration>
I have the following code in my service:
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("test");
The file c:\CGSD\log\logfile.txt is created but nothing is ever written to it.
I've been through the forums all day trying to track this one down, but if I overlooked an already posted solution I apologize.
Your filter level looks to be configured at <levelMin value="INFO" /> but you are testing a log.Debug message. Change your configuration to have <levelMin value="DEBUG" /> and try again. If that doesn't fix it, there may be other config problems also.
You require a minimum level INFO and are trying to log a message with level DEBUG. As you can see in the documentation (http://logging.apache.org/log4net/release/manual/introduction.html), the priority of DEBUG is lower than INFO so your message is filtered out.

Categories