/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
using System;
using System.Data;
#if NETSTANDARD1_3
using System.Reflection;
#endif
using System.Xml;
using log4net.Appender;
using log4net.Config;
#if !NETSTANDARD1_3
using log4net.Core;
#endif
using log4net.Layout;
using log4net.Repository;
using log4net.Tests.Appender.AdoNet;
using log4net.Util;
using NUnit.Framework;
namespace log4net.Tests.Appender
{
[TestFixture]
public class AdoNetAppenderTest
{
[Test]
public void NoBufferingTest()
{
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
AdoNetAppender adoNetAppender = new AdoNetAppender();
adoNetAppender.BufferSize = -1;
#if NETSTANDARD1_3
adoNetAppender.ConnectionType = typeof(Log4NetConnection).AssemblyQualifiedName;
#else
adoNetAppender.ConnectionType = "log4net.Tests.Appender.AdoNet.Log4NetConnection";
#endif
adoNetAppender.ActivateOptions();
BasicConfigurator.Configure(rep, adoNetAppender);
ILog log = LogManager.GetLogger(rep.Name, "NoBufferingTest");
log.Debug("Message");
Assert.NotNull(Log4NetCommand.MostRecentInstance);
Assert.AreEqual(1, Log4NetCommand.MostRecentInstance.ExecuteNonQueryCount);
}
[Test]
public void BufferingTest()
{
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
int bufferSize = 5;
AdoNetAppender adoNetAppender = new AdoNetAppender();
adoNetAppender.BufferSize = bufferSize;
#if NETSTANDARD1_3
adoNetAppender.ConnectionType = typeof(Log4NetConnection).AssemblyQualifiedName;
#else
adoNetAppender.ConnectionType = "log4net.Tests.Appender.AdoNet.Log4NetConnection";
#endif
adoNetAppender.ActivateOptions();
BasicConfigurator.Configure(rep, adoNetAppender);
ILog log = LogManager.GetLogger(rep.Name, "BufferingTest");
for (int i = 0; i < bufferSize; i++)
{
log.Debug("Message");
Assert.IsNull(Log4NetCommand.MostRecentInstance);
}
log.Debug("Message");
Assert.NotNull(Log4NetCommand.MostRecentInstance);
Assert.AreEqual(bufferSize+1, Log4NetCommand.MostRecentInstance.ExecuteNonQueryCount);
}
#if !NETSTANDARD1_3
[Test]
public void WebsiteExample()
{
XmlDocument log4netConfig = new XmlDocument();
#region Load log4netConfig
log4netConfig.LoadXml(@"
");
#endregion
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
XmlConfigurator.Configure(rep, log4netConfig["log4net"]);
ILog log = LogManager.GetLogger(rep.Name, "WebsiteExample");
log.Debug("Message");
IDbCommand command = Log4NetCommand.MostRecentInstance;
Assert.NotNull(command);
Assert.AreEqual(
"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)",
command.CommandText);
Assert.AreEqual(6, command.Parameters.Count);
IDbDataParameter param = (IDbDataParameter)command.Parameters["@message"];
Assert.AreEqual("Message", param.Value);
param = (IDbDataParameter)command.Parameters["@log_level"];
Assert.AreEqual(Level.Debug.ToString(), param.Value);
param = (IDbDataParameter)command.Parameters["@logger"];
Assert.AreEqual("WebsiteExample", param.Value);
param = (IDbDataParameter)command.Parameters["@exception"];
Assert.IsEmpty((string)param.Value);
}
[Test]
public void BufferingWebsiteExample()
{
XmlDocument log4netConfig = new XmlDocument();
#region Load log4netConfig
log4netConfig.LoadXml(@"
");
#endregion
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
XmlConfigurator.Configure(rep, log4netConfig["log4net"]);
ILog log = LogManager.GetLogger(rep.Name, "WebsiteExample");
for (int i = 0; i < 3; i++)
{
log.Debug("Message");
}
IDbCommand command = Log4NetCommand.MostRecentInstance;
Assert.NotNull(command);
Assert.AreEqual(
"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)",
command.CommandText);
Assert.AreEqual(6, command.Parameters.Count);
IDbDataParameter param = (IDbDataParameter)command.Parameters["@message"];
Assert.AreEqual("Message", param.Value);
param = (IDbDataParameter)command.Parameters["@log_level"];
Assert.AreEqual(Level.Debug.ToString(), param.Value);
param = (IDbDataParameter)command.Parameters["@logger"];
Assert.AreEqual("WebsiteExample", param.Value);
param = (IDbDataParameter)command.Parameters["@exception"];
Assert.IsEmpty((string)param.Value);
}
#endif
[Test]
public void NullPropertyXmlConfig()
{
XmlDocument log4netConfig = new XmlDocument();
#region Load log4netConfig
log4netConfig.LoadXml(@"
");
#endregion
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
XmlConfigurator.Configure(rep, log4netConfig["log4net"]);
ILog log = LogManager.GetLogger(rep.Name, "NullPropertyXmlConfig");
log.Debug("Message");
IDbCommand command = Log4NetCommand.MostRecentInstance;
Assert.NotNull(command);
IDbDataParameter param = (IDbDataParameter)command.Parameters["@productId"];
Assert.AreNotEqual(SystemInfo.NullText, param.Value);
Assert.AreEqual(DBNull.Value, param.Value);
}
[Test]
public void NullPropertyProgmaticConfig()
{
AdoNetAppenderParameter productIdParam = new AdoNetAppenderParameter();
productIdParam.ParameterName = "@productId";
productIdParam.DbType = DbType.String;
productIdParam.Size = 50;
RawPropertyLayout rawPropertyLayout = new RawPropertyLayout();
rawPropertyLayout.Key = "ProductId";
productIdParam.Layout = rawPropertyLayout;
AdoNetAppender appender = new AdoNetAppender();
#if NETSTANDARD1_3
appender.ConnectionType = typeof(Log4NetConnection).AssemblyQualifiedName;
#else
appender.ConnectionType = typeof(Log4NetConnection).FullName;
#endif
appender.BufferSize = -1;
appender.CommandText = "INSERT INTO Log ([productId]) VALUES (@productId)";
appender.AddParameter(productIdParam);
appender.ActivateOptions();
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, appender);
ILog log = LogManager.GetLogger(rep.Name, "NullPropertyProgmaticConfig");
log.Debug("Message");
IDbCommand command = Log4NetCommand.MostRecentInstance;
Assert.NotNull(command);
IDbDataParameter param = (IDbDataParameter)command.Parameters["@productId"];
Assert.AreNotEqual(SystemInfo.NullText, param.Value);
Assert.AreEqual(DBNull.Value, param.Value);
}
}
}