my code seems to be working but it does not write anything to my logs file I edited the location of files but it still does not work I don't know where my errors are because it does not pop up
my this is the contents of my app config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
<configSections>
<section name ="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="ALL"></level>
<appender-ref ref ="AdoNetAppender"></appender-ref>
</root>
<appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
<connectionString value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Toshiii\Desktop\V#\Logs.accdb;User Id=;Password=;" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (#log_date, #thread, #log_level, #logger, #message)" />
<parameter>
<parameterName value="#log_date" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date" />
</layout>
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="#log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="1024" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
</log4net>
and the contents of my system codes are
using log4net;
private void q_Click(object sender, EventArgs e)
{
log.Info("Your data has been successfully saved");
}
private void w_Click_1(object sender, EventArgs e)
{
int x = 0;
try
{
int y = 1 / x;
}
catch(Exception ex)
{
log.Error(ex.Message, ex);
}
}
what I want to see is that there are logs in my database, and I also did create a file that has the values from above so I'm wondering where are the problem in my codes
Related
I have been using log4net to log to an Oracle database for the past few weeks and everything was working until I was told that Native Network Encryption had been turn on. This caused my logging to cease and I'm clueless on how to fix it. No errors are given, it just won't write to the DB. I am using the following logger.xml file:
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<connectionStringName value="Payments" />
<commandText value="INSERT INTO CROWN.EOB_LOG (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<bufferSize value="1" />
<parameter>
<parameterName value=":log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value=":log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender_Oracle" />
</root>
</log4net>
As I said this was working perfectly until NNE was turned on. The application I am using is a C# console app. Can anyone point me in the right direction?
I have been trying to log the error in my .Net website to the SQL database using Log4Net AdoNetAppender, but the error message are not getting logged in the database. Looks like the AdoNetAppender is not getting enabled or read. While debugging what I observed is that the Logger.Log shows all its properties values as false. i.e. IsErrorEnabled / IsDebugEnabled / IsFatalEnabled / IsInfoEnabled / IsWarnEnabled all as false.
Version of the log4net.dll I am using is 2.0.8.0
click here to view image of object properties
The logging.cs code is as below:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Utility.Library
{
public class Logging
{
public static class Logger
{
public static ILog Log = LogManager.GetLogger(typeof(SharedMethods));
}
public static class LogException
{
private static StringBuilder sessionJobLogBuilder = new StringBuilder();
public static void LogError(string sMsg, Exception ex)
{
Logger.Log.Error(sMsg, ex);
sessionJobLogBuilder.AppendLine(sMsg + Environment.NewLine);
sessionJobLogBuilder.AppendLine(ex.ToString() + Environment.NewLine);
}
public static void LogInfo(string sMsg)
{
Logger.Log.Info(sMsg);
sessionJobLogBuilder.AppendLine(sMsg + Environment.NewLine);
}
}
}
}
And below is the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection,
System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=xxx\xxx;
initial catalog=xxx;integrated security=false;
persist security info=True;User ID=xxx;Password=xxx" />
<commandText value="EXECUTE [dbo].[WriteLogEntry] #App, #Module, #Ver, #Computer, #UserID, #Level, #Message, #Exception" />
<parameter>
<parameterName value="#App" />
<dbType value="String" />
<size value="30" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%App" />
</layout>
</parameter>
<parameter>
<parameterName value="#Module" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%Module" />
</layout>
</parameter>
<parameter>
<parameterName value="#Ver" />
<dbType value="String" />
<size value="20" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%Ver" />
</layout>
</parameter>
<parameter>
<parameterName value="#Computer" />
<dbType value="String" />
<size value="15" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%Computer" />
</layout>
</parameter>
<parameter>
<parameterName value="#UserID" />
<dbType value="String" />
<size value="30" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%UserID" />
</layout>
</parameter>
<parameter>
<parameterName value="#Level" />
<dbType value="Int32" />
<layout type="log4net.Layout.PatternLayout" />
</parameter>
<parameter>
<parameterName value="#Message" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%Message" />
</layout>
</parameter>
<parameter>
<parameterName value="#Exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
</configuration>
But the Logger.Log properties at all levels show as false.
From your comment I read you have a separate app.config file and the Logger class defined in a separate assembly.
An ASP.NET website only considers the web.config file.
Your app.config file doesn't get loaded, so the Log4net configuration doesn't get applied.
One way to solve this is to transform your app.config file into a separate Log4netconfiguration file and to load that explicitly.
Move the <log4net> ... </log4net> content from the app.config file to a separate file (by convention named Log4net.config) in the root of your website project.
The <log4net> element must be the root xml element in that file.
Remove the [assembly: log4net.Config.XmlConfigurator(Watch = true)] from your Logger project.
Add the following code to Application_Start in Global.asax of the website project to ensure that the log4net configuration gets loaded at website startup phase.
void Application_Start(object sender, EventArgs e)
{
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetExecutingAssembly());
XmlConfigurator.Configure(repository, new FileInfo(Server.MapPath("log4net.config")));
// Remaining startup code.
}
I have the following log4net.config in the root of my website project (near web.config) with just a RollingFileAppender but the concept is the same.
<log4net>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<param name="ImmediateFlush" value="true" />
<file value="Log.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="file" />
</root>
</log4net>
With your AdoNetAppender setup in my Log4net.config the expected values for IsDebugEnabled, IsErrorEnabled, etc. get shown.
Summary:
Log4net's internal debug logging showed that the configuration file was not being loaded from the presumed location.
The combination of correcting this location (in the XmlConfigurator.Configure method call) and providing a configuration as shown above solved the problem.
I am currently using Log4net to log in a table but I need to stop using that table and log everything in another table for logging errors. Basically I just changed the log4net values and parameters and the table in which I am logging. Here is the Log file
<!--SQL LOG TABLE Appender-->
<appender name="LogAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<!--THIS SETTING ALTERS HOW OFTEN IT WRITES ALL THE LOGGING TO THE DB-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!--local logging connection string-->
<connectionStringName value="DSFDefaultConnection" />
<commandText value="INSERT INTO ProviderHistoryLog ([LogDate],[Event], [CalledPackageName],[ActivityDescription],[SubPackageTaskName],[TaskName]) VALUES (#log_date, #thread, #logger, #message, #notApplicable, #requesturi)" />
<parameter>
<parameterName value="#ProviderId" />
<dbType value="long" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="null" />
</layout>
</parameter>
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawUtcTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger | %class " />
</layout>
</parameter>
<!--<parameter>
<parameterName value="#identity" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="DPPAdmin#aez.com"/>-->
<!-- <converter>-->
<!-- <name value="httpuser" />-->
<!-- <type value="fi360.DataServiceFramework.Common.Logging.HttpIdentityUserPatternConverter" />-->
<!-- </converter>-->
<!-- </layout>-->
<!-- </parameter>-->
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="#notApplicable" />
<dbType value="String" />
<size value="-1" />
<layout type="%notApplicable" />
</parameter>
<parameter>
<parameterName value="#requesturi" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%requesturi"/>
<converter>
<name value="requesturi" />
<type value="fi360.DataServiceFramework.Common.Logging.RequestUriUserPatternConverter" />
</converter>
</layout>
</parameter>
</appender>
The problem is, it's not logging in the ProviderHistoryLog table. It's not logging anything. As you can see the value for the buffer size is 1 so I am thinking the issue is in the INSERT INTO statement.
I want use Log4Net in my application for logging to console and database(Oracle)
I made this setting :
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] - Level%newline - %level%newlineMessage - %message%newlineUTC Date - %utcdate{ABSOLUTE}%newlineDate - %date{ABSOLUTE}%newline%newline "/>
</layout>
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
<connectionString value="DATA SOURCE= (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myIpAddress)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xe))); USER ID=user_name PASSWORD=my_password;"/>
<commandText value="INSERT INTO LOG (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<bufferSize value="1" />
<parameter>
<parameterName value=":log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value=":log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<root>
<level value="All"/>
<appender-ref ref="AdoNetAppender_Oracle"/>
<appender-ref ref="ConsoleAppender"/>
</root>
On console I have output, but after start there is some Exception thrown: 'Oracle.ManagedDataAccess.Client.OracleException' in Oracle.ManagedDataAccess.dll
And of course there is no new line in Oracle table.
Any suggestion why not working ?
Thank you.
This is a working configuration for my custom table but you can use the default columns. Pay attention on which OracleDataAccess use in your application (Version=2.121.2.0) Check here AdoNetAppender.ConnectionType Property
<appender name="DatabaseAppender" type="log4net.Appender.AdoNetAppender">
<connectionString value="Data Source=HR;User Id=HRL;Password=HR;Max Pool Size=30" />
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<commandText value="INSERT INTO HRPORTAL.LOG_WEB (DATA,SESSIONE,ENTITA,DESCRIZIONE,DETTAGLIO,MODULO) VALUES (:data, :sessione,:entita,:descrizione, :dettaglio,:modulo)" />
<bufferSize value="1" />
<parameter>
<parameterName value=":data" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":sessione" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{sessione}" />
</layout>
</parameter>
<parameter>
<parameterName value=":entita" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{entita}" />
</layout>
</parameter>
<parameter>
<parameterName value=":descrizione" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{descrizione}" />
</layout>
</parameter>
<parameter>
<parameterName value=":dettaglio" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{dettaglio}" />
</layout>
</parameter>
<parameter>
<parameterName value=":modulo" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{modulo}" />
</layout>
</parameter>
</appender>
<root >
<level value="ALL" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
I have a webapp that I would like to use a different log4net config file when running under DEBUG mode compared to running under RELEASE mode.
I am using log4net version 2.0.3.
In my AssemblyInfo.cs class I have this code:
#if DEBUG
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4netDebug.xml", Watch = true)]
#else
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4netLive.xml", Watch = true)]
#endif
In my Global.asax.cs class I have this code:
log4net.Config.XmlConfigurator.Configure();
log = LogManager.GetLogger(typeof(GlobalApp));
(log is a public static member of GlobalApp.)
In my log4netDebug.xml file I have this code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Configuration for logging using log4net-->
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!-- Show logging in the Visual Studio output window-->
<appender name="TraceAppender"
type="log4net.Appender.TraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-6level - %message%newline" />
</layout>
</appender>
<appender
name="AdoNetAppender"
type="log4net.Appender.AdoNetAppender">
<threshold>INFO</threshold>
<bufferSize
value="1" />
<connectionType
value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=BLAH;initial catalog=BLAH;integrated security=True;persist security info=True" />
<commandText value="INSERT INTO Logs ([Date],[Thread],[Source],[Level], [Logger],[Message],[Exception],[HostName]) VALUES (#log_date, #thread, 'LOG SOURCE',#log_level, #logger, #message, #exception, #hostname)" />
<parameter>
<parameterName value="#log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="#thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="#hostname" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}" />
</layout>
</parameter>
<parameter>
<parameterName value="#log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="#logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="#message" />
<dbType value="String" />
<size value="-1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="#exception" />
<dbType value="String" />
<size value="-1" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ALL" />
<!-- Show logging in the Visual Studio output window-->
<appender-ref ref="TraceAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
</configuration>
I have tested this config script and it works if I place it in my web.config file.
The log4netDebug.xml file has the Copy always property set.
When I try to do something like this:
System.IO.FileInfo fi = new System.IO.FileInfo("Log4NetDebug.xml");
log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);
This doesn't work either.
I have read several stackoverflow and other web pages that describe how to do this and I seem to be doing what they recommend doing but it still deson't seem to work.
Any ideas why?
If you look at the Constructor for the FileInfo class, you will see that it expects The fully qualified name of the new file, or the relative file name..
So when you do this System.IO.FileInfo fi = new System.IO.FileInfo("Log4NetDebug.xml"); you need to specify the full path to the file.
As this is in a web app, you probably want something like this:
var logFile = new FileInfo(Path.Combine(HttpRuntime.BinDirectory, "Log4NetDebug.xml"));