Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:22:29+00:00 2026-05-19T04:22:29+00:00

Earlier today I asked a question about configuring log4net from code and got an

  • 0

Earlier today I asked a question about configuring log4net from code and got an answer very quickly which allowed me to configure it to output to a text file. Since then my needs have changed and I need to use SqLite as the appender. So I created the following class to allow this:

public static class SqLiteAppender
{
    public static IAppender GetSqliteAppender(string dbFilename)
    {
        var dbFile = new FileInfo(dbFilename);

    if (!dbFile.Exists)
    {
        CreateLogDb(dbFile);
    }

    var appender = new AdoNetAppender
                       {
                           ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite",
                           ConnectionString = String.Format("Data Source={0};Version=3;", dbFilename),
                           CommandText = "INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)"
                       };

    appender.AddParameter(new AdoNetAppenderParameter
                              {
                                  ParameterName = "@Date",
                                  DbType = DbType.DateTime,
                                  Layout = new log4net.Layout.RawTimeStampLayout()

                              });

    appender.AddParameter(new AdoNetAppenderParameter
                              {
                                  ParameterName = "@Level",
                                  DbType = DbType.String,
                                  Layout = new log4net.Layout.RawPropertyLayout { Key = "Level" }
                              });

    appender.AddParameter(new AdoNetAppenderParameter
                              {
                                  ParameterName = "@Logger",
                                  DbType = DbType.String,
                                  Layout = new log4net.Layout.RawPropertyLayout { Key = "LoggerName" }
                              });

    appender.AddParameter(new AdoNetAppenderParameter
                              {
                                  ParameterName = "@Message",
                                  DbType = DbType.String,
                                  Layout = new log4net.Layout.RawPropertyLayout { Key = "RenderedMessage" }
                              });

    appender.BufferSize = 100;
    appender.ActivateOptions();
    return appender;
}

public static void CreateLogDb(FileInfo file)
{
    using (var conn = new SQLiteConnection())
    {
        conn.ConnectionString = string.Format("Data Source={0};New=True;Compress=True;Synchronous=Off", file.FullName);
        conn.Open();
        var cmd = conn.CreateCommand();

        cmd.CommandText =
                         @"CREATE TABLE Log(
                            LogId     INTEGER PRIMARY KEY,
                            Date      DATETIME NOT NULL,
                            Level     VARCHAR(50) NOT NULL,
                            Logger    VARCHAR(255) NOT NULL,
                            Message   TEXT DEFAULT NULL
                        );";

        cmd.ExecuteNonQuery();
        cmd.Dispose();
        conn.Close();
    }
}

}

The problem is that although the database is created and the table added, I am getting no logging to this.

The class is used like this:

BasicConfigurator.Configure(SqLiteAppender.GetSqliteAppender(applicationContext.GetLogFile().FullName));

any help to point me in the right direction would be appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:22:30+00:00Added an answer on May 19, 2026 at 4:22 am

    The problem is in the RawPropertyLayout instances. In my testing, they don’t pull out the Level and LoggerName properties as one would expect, which results in null constraint violations on the database. These can be fixed by using a PatternLayout as follows:

    Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level"))
    

    and

    Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger"))
    

    Here’s a complete working example:

    using System;
    using System.Data;
    using System.Data.SQLite;
    using System.IO;
    using log4net;
    using log4net.Appender;
    using log4net.Config;
    using log4net.Layout;
    
    namespace ConsoleApplication1
    {
        class SQLiteLogging
        {
            public static void Test()
            {
                BasicConfigurator.Configure(SqLiteAppender.GetSqliteAppender("D:/test.dat"));
                LogManager.GetLogger(typeof (SqLiteAppender)).Info("Hello there");
            }
    
            public static class SqLiteAppender
            {
                public static IAppender GetSqliteAppender(string dbFilename)
                {
                    var dbFile = new FileInfo(dbFilename);
    
                    if (!dbFile.Exists)
                    {
                        CreateLogDb(dbFile);
                    }
    
                    var appender = new AdoNetAppender
                                       {
                                           ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite",
                                           ConnectionString = String.Format("Data Source={0};Version=3;", dbFilename),
                                           CommandText = "INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)"
                                       };
    
                    appender.AddParameter(new AdoNetAppenderParameter
                                              {
                                                  ParameterName = "@Date",
                                                  DbType = DbType.DateTime,
                                                  Layout = new RawTimeStampLayout()
    
                                              });
    
                    appender.AddParameter(new AdoNetAppenderParameter
                                              {
                                                  ParameterName = "@Level",
                                                  DbType = DbType.String,
                                                  Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level"))
                                              });
    
                    appender.AddParameter(new AdoNetAppenderParameter
                                              {
                                                  ParameterName = "@Logger",
                                                  DbType = DbType.String,
                                                  Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger"))
                                              });
    
                    appender.AddParameter(new AdoNetAppenderParameter
                                              {
                                                  ParameterName = "@Message",
                                                  DbType = DbType.String,
                                                  Layout = new RawPropertyLayout { Key = "RenderedMessage" }
                                              });
    
                    appender.ActivateOptions();
                    return appender;
                }
    
                public static void CreateLogDb(FileInfo file)
                {
                    using (var conn = new SQLiteConnection())
                    {
                        conn.ConnectionString = string.Format("Data Source={0};New=True;Compress=True;Synchronous=Off", file.FullName);
                        conn.Open();
                        var cmd = conn.CreateCommand();
    
                        cmd.CommandText =
                                         @"CREATE TABLE Log(
                                LogId     INTEGER PRIMARY KEY,
                                Date      DATETIME NOT NULL,
                                Level     VARCHAR(50) NOT NULL,
                                Logger    VARCHAR(255) NOT NULL,
                                Message   TEXT DEFAULT NULL
                            );";
    
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        conn.Close();
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Earlier today I asked this question which arose from A- My poor planning and
Earlier today I asked a question about environ , and one of the more
Earlier today I asked a question about passing dictionary values to a function. While
Earlier today, I asked a question about the way Python handles certain kinds of
I asked a question earlier today about singletons, and I'm having some difficulties understanding
Earlier today a question was asked regarding input validation strategies in web apps .
Earlier I asked a question about why I see so many examples use the
I asked this question in the DataDynamics forum earlier today. I thought that maybe
This is a follow-up to another question I asked earlier today. I am creating
earlier today I asked this question . So since moq creates it's own class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.