From 0c87b6160539e0c61efdbaa6e622d9c90898aad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=8B=BE=E7=8E=96?= Date: Sun, 27 Nov 2022 03:41:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add=20SQLite=20demo=20framework4.6=EF=BC=8C?= =?UTF-8?q?framework4.8,=20Net=20Core=203.1=EF=BC=8CNet=205=EF=BC=8CNet=20?= =?UTF-8?q?6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MicroKnights.Log4NetAdoNetAppender.sln | 52 +++++- .../App.config | 6 + .../Log4NetAdoNetAppender.Net46.Demo.csproj | 175 ++++++++++++++++++ .../Program.cs | 113 +++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++ .../connectionstrings.json | 5 + .../log4net.config | 113 +++++++++++ .../packages.config | 68 +++++++ .../App.config | 6 + .../Log4NetAdoNetAppender.Net48.Demo.csproj | 135 ++++++++++++++ .../Program.cs | 15 ++ .../Properties/AssemblyInfo.cs | 36 ++++ .../packages.config | 27 +++ ...og4NetAdoNetAppender.NetCore31.Demo.csproj | 28 +++ .../Program.cs | 113 +++++++++++ .../connectionstrings.json | 5 + .../log4net.config | 113 +++++++++++ ...og4NetAdoNetAppender.NetCore50.Demo.csproj | 21 +++ .../Program.cs | 113 +++++++++++ .../log4net.config | 113 +++++++++++ ...og4NetAdoNetAppender.NetCore60.Demo.csproj | 20 ++ .../Program.cs | 113 +++++++++++ .../log4net.config | 113 +++++++++++ 23 files changed, 1537 insertions(+), 2 deletions(-) create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Properties/AssemblyInfo.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/connectionstrings.json create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/log4net.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/packages.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Properties/AssemblyInfo.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/packages.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Log4NetAdoNetAppender.NetCore31.Demo.csproj create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Program.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/connectionstrings.json create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/log4net.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Log4NetAdoNetAppender.NetCore50.Demo.csproj create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Program.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/log4net.config create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Log4NetAdoNetAppender.NetCore60.Demo.csproj create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Program.cs create mode 100644 demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/log4net.config diff --git a/MicroKnights.Log4NetAdoNetAppender.sln b/MicroKnights.Log4NetAdoNetAppender.sln index 2872c9e..baa9683 100644 --- a/MicroKnights.Log4NetAdoNetAppender.sln +++ b/MicroKnights.Log4NetAdoNetAppender.sln @@ -1,12 +1,30 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29509.3 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32630.192 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroKnights.Log4NetAdoNetAppender", "src\MicroKnights.Log4NetAdoNetAppender.csproj", "{CD00FEEF-8E51-4A13-8D1D-4A2712AFA643}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroKnights.Log4NetAdoNetAppender.Test", "test\MicroKnights.Log4NetAdoNetAppender.Test.csproj", "{76EE2EB0-B554-467F-8A11-C1BCC6ECE270}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MicroKnights.Log4NetAdoNetAppender.Demo", "MicroKnights.Log4NetAdoNetAppender.Demo", "{BFFAEA7E-2735-439D-80AB-642D1EF14B67}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SQLite", "SQLite", "{A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Log4NetAdoNetAppender.NetCore31.Demo", "demo\SQLite\Log4NetAdoNetAppender.NetCore31.Demo\Log4NetAdoNetAppender.NetCore31.Demo.csproj", "{E15D54DE-269A-49D7-8628-3526832745E9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Log4NetAdoNetAppender.NetCore50.Demo", "demo\SQLite\Log4NetAdoNetAppender.NetCore50.Demo\Log4NetAdoNetAppender.NetCore50.Demo.csproj", "{CD4C87B9-B77D-474A-A97A-7A466416EEA9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Log4NetAdoNetAppender.NetCore60.Demo", "demo\SQLite\Log4NetAdoNetAppender.NetCore60.Demo\Log4NetAdoNetAppender.NetCore60.Demo.csproj", "{BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SqlServer", "SqlServer", "{C5B26F8C-38DE-41DE-A6B6-1EAC9343C3D2}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MySql", "MySql", "{9F6F7EFE-A9F2-476E-978C-8EED85EEE89D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Log4NetAdoNetAppender.Net46.Demo", "demo\SQLite\Log4NetAdoNetAppender.Net46.Demo\Log4NetAdoNetAppender.Net46.Demo.csproj", "{204332A9-D680-40BD-ACED-91A5A223E49E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Log4NetAdoNetAppender.Net48.Demo", "demo\SQLite\Log4NetAdoNetAppender.Net48.Demo\Log4NetAdoNetAppender.Net48.Demo.csproj", "{49E25F54-A9EB-4821-A60E-D9AF3685C8C1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,10 +39,40 @@ Global {76EE2EB0-B554-467F-8A11-C1BCC6ECE270}.Debug|Any CPU.Build.0 = Debug|Any CPU {76EE2EB0-B554-467F-8A11-C1BCC6ECE270}.Release|Any CPU.ActiveCfg = Release|Any CPU {76EE2EB0-B554-467F-8A11-C1BCC6ECE270}.Release|Any CPU.Build.0 = Release|Any CPU + {E15D54DE-269A-49D7-8628-3526832745E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E15D54DE-269A-49D7-8628-3526832745E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E15D54DE-269A-49D7-8628-3526832745E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E15D54DE-269A-49D7-8628-3526832745E9}.Release|Any CPU.Build.0 = Release|Any CPU + {CD4C87B9-B77D-474A-A97A-7A466416EEA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD4C87B9-B77D-474A-A97A-7A466416EEA9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD4C87B9-B77D-474A-A97A-7A466416EEA9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD4C87B9-B77D-474A-A97A-7A466416EEA9}.Release|Any CPU.Build.0 = Release|Any CPU + {BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F}.Release|Any CPU.Build.0 = Release|Any CPU + {204332A9-D680-40BD-ACED-91A5A223E49E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {204332A9-D680-40BD-ACED-91A5A223E49E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {204332A9-D680-40BD-ACED-91A5A223E49E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {204332A9-D680-40BD-ACED-91A5A223E49E}.Release|Any CPU.Build.0 = Release|Any CPU + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} = {BFFAEA7E-2735-439D-80AB-642D1EF14B67} + {E15D54DE-269A-49D7-8628-3526832745E9} = {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} + {CD4C87B9-B77D-474A-A97A-7A466416EEA9} = {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} + {BEE3E4D9-2704-4D63-8E2B-57DAAE816E6F} = {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} + {C5B26F8C-38DE-41DE-A6B6-1EAC9343C3D2} = {BFFAEA7E-2735-439D-80AB-642D1EF14B67} + {9F6F7EFE-A9F2-476E-978C-8EED85EEE89D} = {BFFAEA7E-2735-439D-80AB-642D1EF14B67} + {204332A9-D680-40BD-ACED-91A5A223E49E} = {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1} = {A63C35F8-DC2C-474F-8CD1-7D5E3B7985AD} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {526609E5-2C9C-4391-B782-007E35268534} EndGlobalSection diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config new file mode 100644 index 0000000..8324aa6 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj new file mode 100644 index 0000000..504d390 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj @@ -0,0 +1,175 @@ + + + + + + Debug + AnyCPU + {204332A9-D680-40BD-ACED-91A5A223E49E} + Exe + Log4NetAdoNetAppender.Net46.Demo + Log4NetAdoNetAppender.Net46.Demo + v4.6 + 512 + true + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\log4net.2.0.15\lib\net45\log4net.dll + + + ..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetAdoNetAppender.dll + + + ..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetHelper.dll + + + ..\packages\Microsoft.Data.Sqlite.1.0.0\lib\net451\Microsoft.Data.Sqlite.dll + + + ..\packages\Microsoft.Extensions.Configuration.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll + + + ..\packages\Microsoft.Extensions.Configuration.Abstractions.1.1.2\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Configuration.FileExtensions.1.1.2\lib\net451\Microsoft.Extensions.Configuration.FileExtensions.dll + + + ..\packages\Microsoft.Extensions.Configuration.Json.1.1.2\lib\net451\Microsoft.Extensions.Configuration.Json.dll + + + ..\packages\Microsoft.Extensions.FileProviders.Abstractions.1.1.1\lib\netstandard1.0\Microsoft.Extensions.FileProviders.Abstractions.dll + + + ..\packages\Microsoft.Extensions.FileProviders.Physical.1.1.1\lib\net451\Microsoft.Extensions.FileProviders.Physical.dll + + + ..\packages\Microsoft.Extensions.FileSystemGlobbing.1.1.1\lib\net45\Microsoft.Extensions.FileSystemGlobbing.dll + + + ..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Primitives.1.1.1\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll + + + ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll + + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + + + + ..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll + + + + + ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + + + + ..\packages\System.Data.Common.4.3.0\lib\net451\System.Data.Common.dll + + + ..\packages\System.Data.SqlClient.4.4.2\lib\net46\System.Data.SqlClient.dll + + + ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll + + + ..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + + + + ..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll + + + ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + + + ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + + + ..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + + + ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll + + + + + + + + + ..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + + + + + + + + + + Always + + + Always + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs new file mode 100644 index 0000000..0054c09 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs @@ -0,0 +1,113 @@ +using log4net; +using log4net.Config; +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using log4net.Util; +using Microsoft.Data.Sqlite; +using MicroKnights.Log4NetHelper; + +namespace Log4NetAdoNetAppender.Net46.Demo +{ + class Program + { + static async Task Main(string[] args) + { + var connectionString = "Data Source=\"./log4net.db\""; + + // Create Log Table + //创建表格 + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + + //Create a table (if it already exists, delete it first) + //创建一个表(如果已经存在,则先删除) + Console.WriteLine("Delete Table Log ......"); + Console.WriteLine("删除日志表 ......"); + SqliteCommand delTableCmd = connection.CreateCommand(); + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + Console.WriteLine("Create Table Log ......"); + Console.WriteLine("创建日志表 ......"); + SqliteCommand createTableCmd = connection.CreateCommand(); + createTableCmd.CommandText = "CREATE TABLE Log (" + + "Id INTEGER PRIMARY KEY," + + "Date DATETIME NOT NULL," + + "Thread VARCHAR(255) NOT NULL," + + "Level VARCHAR(50) NOT NULL," + + "Logger VARCHAR(255) NOT NULL," + + "Message TEXT DEFAULT NULL," + + "Exception TEXT DEFAULT NULL" + + "); "; + createTableCmd.ExecuteNonQuery(); + } + Console.WriteLine("Create Table Log Success ......"); + // Load configuration + const string Log4netConfigFilename = "log4net.config"; + + if (File.Exists(Log4netConfigFilename) == false) + { + throw new FileNotFoundException($"{Log4netConfigFilename} not found", Log4netConfigFilename); + } + +#if DEBUG + InternalDebugHelper.EnableInternalDebug(delegate (object source, LogReceivedEventArgs eventArgs) + { + Console.WriteLine(eventArgs.LogLog.Message); + if (eventArgs.LogLog.Exception != null) + Console.WriteLine(eventArgs.LogLog.Exception.StackTrace); + }); +#endif + + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo(Log4netConfigFilename)); + + var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + // Analog input some logs + //模拟输入一些日志 + log.Info("This is log information of an analog input info (Info)"); + Console.WriteLine("这是模拟输入信息(info)的日志信息"); + log.Debug("This is log information of an analog input info (Debug)"); + Console.WriteLine("这是模拟输入信息(Debug)的日志信息"); + log.Warn("This is log information of an analog input info (Warn)"); + Console.WriteLine("这是模拟输入信息(Warn)的日志信息"); + log.Error("This is log information of an analog input info (Error)"); + Console.WriteLine("这是模拟输入信息(Error)的日志信息"); + + + // Show logged data from the database + //显示数据库中记录的数据 + await ShowLogDataFromDatabaseWith(connectionString); + + Console.WriteLine("Press Enter to continue"); + Console.WriteLine("按Enter键继续"); + Console.ReadLine(); + } + + private static async Task ShowLogDataFromDatabaseWith(string connectionString) + { + var connection = new SqliteConnection(connectionString); + + connection.Open(); + + var selectCmd = connection.CreateCommand(); + + selectCmd.CommandText = "select * from Log"; + + var reader = await selectCmd.ExecuteReaderAsync(); + + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } + } + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Properties/AssemblyInfo.cs b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0cb725b --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Log4NetAdoNetAppender.Net46.Demo")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Log4NetAdoNetAppender.Net46.Demo")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("204332a9-d680-40bd-aced-91a5a223e49e")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/connectionstrings.json b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/connectionstrings.json new file mode 100644 index 0000000..71a3c2d --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/connectionstrings.json @@ -0,0 +1,5 @@ +{ + "connectionstrings": { + "sqlite": "Data Source=\"./log4net.db\"" + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/log4net.config b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/log4net.config new file mode 100644 index 0000000..9d6a2bb --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/log4net.config @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/packages.config b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/packages.config new file mode 100644 index 0000000..00c3625 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/packages.config @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj new file mode 100644 index 0000000..c99c2ce --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj @@ -0,0 +1,135 @@ + + + + + Debug + AnyCPU + {49E25F54-A9EB-4821-A60E-D9AF3685C8C1} + Exe + Log4NetAdoNetAppender.Net48.Demo + Log4NetAdoNetAppender.Net48.Demo + v4.8 + 512 + true + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\log4net.2.0.15\lib\net45\log4net.dll + + + ..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetAdoNetAppender.dll + + + ..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetHelper.dll + + + ..\packages\Microsoft.Data.Sqlite.Core.3.1.9\lib\netstandard2.0\Microsoft.Data.Sqlite.dll + + + ..\packages\Microsoft.Extensions.Configuration.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll + + + ..\packages\Microsoft.Extensions.Configuration.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Configuration.FileExtensions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.FileExtensions.dll + + + ..\packages\Microsoft.Extensions.Configuration.Json.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Json.dll + + + ..\packages\Microsoft.Extensions.FileProviders.Abstractions.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Abstractions.dll + + + ..\packages\Microsoft.Extensions.FileProviders.Physical.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Physical.dll + + + ..\packages\Microsoft.Extensions.FileSystemGlobbing.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileSystemGlobbing.dll + + + ..\packages\Microsoft.Extensions.Logging.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Primitives.2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll + + + ..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.batteries_v2.dll + + + ..\packages\SQLitePCLRaw.core.2.0.2\lib\netstandard2.0\SQLitePCLRaw.core.dll + + + ..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.nativelibrary.dll + + + ..\packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.2\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll + + + + ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll + + + + + ..\packages\System.Data.SqlClient.4.4.2\lib\net461\System.Data.SqlClient.dll + + + ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + + + + + + + + + + + + + + + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs new file mode 100644 index 0000000..59ef79e --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Log4NetAdoNetAppender.Net48.Demo +{ + internal class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Properties/AssemblyInfo.cs b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a8c7a2e --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Log4NetAdoNetAppender.Net48.Demo")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Log4NetAdoNetAppender.Net48.Demo")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("49e25f54-a9eb-4821-a60e-d9af3685c8c1")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/packages.config b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/packages.config new file mode 100644 index 0000000..302475c --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/packages.config @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Log4NetAdoNetAppender.NetCore31.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Log4NetAdoNetAppender.NetCore31.Demo.csproj new file mode 100644 index 0000000..43e38e9 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Log4NetAdoNetAppender.NetCore31.Demo.csproj @@ -0,0 +1,28 @@ + + + + Exe + netcoreapp3.1 + + + + False + + + + + + + + + + + + Always + + + Always + + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Program.cs new file mode 100644 index 0000000..c0364da --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/Program.cs @@ -0,0 +1,113 @@ +using log4net; +using log4net.Config; +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using log4net.Util; +using Microsoft.Data.Sqlite; +using MicroKnights.Log4NetHelper; + +namespace Log4NetAdoNetAppender.NetCore31.Demo +{ + class Program + { + static async Task Main(string[] args) + { + var connectionString = "Data Source=\"./log4net.db\""; + + // Create Log Table + //创建表格 + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + + //Create a table (if it already exists, delete it first) + //创建一个表(如果已经存在,则先删除) + Console.WriteLine("Delete Table Log ......"); + Console.WriteLine("删除日志表 ......"); + await using var delTableCmd = connection.CreateCommand(); + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + Console.WriteLine("Create Table Log ......"); + Console.WriteLine("创建日志表 ......"); + await using var createTableCmd = connection.CreateCommand(); + createTableCmd.CommandText = "CREATE TABLE Log (" + + "Id INTEGER PRIMARY KEY," + + "Date DATETIME NOT NULL," + + "Thread VARCHAR(255) NOT NULL," + + "Level VARCHAR(50) NOT NULL," + + "Logger VARCHAR(255) NOT NULL," + + "Message TEXT DEFAULT NULL," + + "Exception TEXT DEFAULT NULL" + + "); "; + createTableCmd.ExecuteNonQuery(); + } + Console.WriteLine("Create Table Log Success ......"); + // Load configuration + const string Log4netConfigFilename = "log4net.config"; + + if (File.Exists(Log4netConfigFilename) == false) + { + throw new FileNotFoundException($"{Log4netConfigFilename} not found", Log4netConfigFilename); + } + +#if DEBUG + InternalDebugHelper.EnableInternalDebug(delegate (object source, LogReceivedEventArgs eventArgs) + { + Console.WriteLine(eventArgs.LogLog.Message); + if (eventArgs.LogLog.Exception != null) + Console.WriteLine(eventArgs.LogLog.Exception.StackTrace); + }); +#endif + + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo(Log4netConfigFilename)); + + var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + // Analog input some logs + //模拟输入一些日志 + log.Info("This is log information of an analog input info (Info)"); + Console.WriteLine("这是模拟输入信息(info)的日志信息"); + log.Debug("This is log information of an analog input info (Debug)"); + Console.WriteLine("这是模拟输入信息(Debug)的日志信息"); + log.Warn("This is log information of an analog input info (Warn)"); + Console.WriteLine("这是模拟输入信息(Warn)的日志信息"); + log.Error("This is log information of an analog input info (Error)"); + Console.WriteLine("这是模拟输入信息(Error)的日志信息"); + + + // Show logged data from the database + //显示数据库中记录的数据 + await ShowLogDataFromDatabaseWith(connectionString); + + Console.WriteLine("Press Enter to continue"); + Console.WriteLine("按Enter键继续"); + Console.ReadLine(); + } + + private static async Task ShowLogDataFromDatabaseWith(string connectionString) + { + using var connection = new SqliteConnection(connectionString); + + connection.Open(); + + await using var selectCmd = connection.CreateCommand(); + + selectCmd.CommandText = "select * from Log"; + + await using var reader = await selectCmd.ExecuteReaderAsync(); + + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } + } + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/connectionstrings.json b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/connectionstrings.json new file mode 100644 index 0000000..71a3c2d --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/connectionstrings.json @@ -0,0 +1,5 @@ +{ + "connectionstrings": { + "sqlite": "Data Source=\"./log4net.db\"" + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/log4net.config b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/log4net.config new file mode 100644 index 0000000..b65d1aa --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore31.Demo/log4net.config @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Log4NetAdoNetAppender.NetCore50.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Log4NetAdoNetAppender.NetCore50.Demo.csproj new file mode 100644 index 0000000..063c779 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Log4NetAdoNetAppender.NetCore50.Demo.csproj @@ -0,0 +1,21 @@ + + + + Exe + net5.0 + + + + + + + + + + + + Always + + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Program.cs new file mode 100644 index 0000000..23c03f8 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/Program.cs @@ -0,0 +1,113 @@ +using log4net; +using log4net.Config; +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using log4net.Util; +using Microsoft.Data.Sqlite; +using MicroKnights.Log4NetHelper; + +namespace Log4NetAdoNetAppender.NetCore50.Demo +{ + class Program + { + static async Task Main(string[] args) + { + var connectionString = "Data Source=\"./log4net.db\""; + + // Create Log Table + //创建表格 + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + + //Create a table (if it already exists, delete it first) + //创建一个表(如果已经存在,则先删除) + Console.WriteLine("Delete Table Log ......"); + Console.WriteLine("删除日志表 ......"); + await using var delTableCmd = connection.CreateCommand(); + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + Console.WriteLine("Create Table Log ......"); + Console.WriteLine("创建日志表 ......"); + await using var createTableCmd = connection.CreateCommand(); + createTableCmd.CommandText = "CREATE TABLE Log (" + + "Id INTEGER PRIMARY KEY," + + "Date DATETIME NOT NULL," + + "Thread VARCHAR(255) NOT NULL," + + "Level VARCHAR(50) NOT NULL," + + "Logger VARCHAR(255) NOT NULL," + + "Message TEXT DEFAULT NULL," + + "Exception TEXT DEFAULT NULL" + + "); "; + createTableCmd.ExecuteNonQuery(); + } + Console.WriteLine("Create Table Log Success ......"); + // Load configuration + const string Log4netConfigFilename = "log4net.config"; + + if (File.Exists(Log4netConfigFilename) == false) + { + throw new FileNotFoundException($"{Log4netConfigFilename} not found", Log4netConfigFilename); + } + +#if DEBUG + InternalDebugHelper.EnableInternalDebug(delegate (object source, LogReceivedEventArgs eventArgs) + { + Console.WriteLine(eventArgs.LogLog.Message); + if (eventArgs.LogLog.Exception != null) + Console.WriteLine(eventArgs.LogLog.Exception.StackTrace); + }); +#endif + + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo(Log4netConfigFilename)); + + var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + // Analog input some logs + //模拟输入一些日志 + log.Info("This is log information of an analog input info (Info)"); + Console.WriteLine("这是模拟输入信息(info)的日志信息"); + log.Debug("This is log information of an analog input info (Debug)"); + Console.WriteLine("这是模拟输入信息(Debug)的日志信息"); + log.Warn("This is log information of an analog input info (Warn)"); + Console.WriteLine("这是模拟输入信息(Warn)的日志信息"); + log.Error("This is log information of an analog input info (Error)"); + Console.WriteLine("这是模拟输入信息(Error)的日志信息"); + + + // Show logged data from the database + //显示数据库中记录的数据 + await ShowLogDataFromDatabaseWith(connectionString); + + Console.WriteLine("Press Enter to continue"); + Console.WriteLine("按Enter键继续"); + Console.ReadLine(); + } + + private static async Task ShowLogDataFromDatabaseWith(string connectionString) + { + using var connection = new SqliteConnection(connectionString); + + connection.Open(); + + await using var selectCmd = connection.CreateCommand(); + + selectCmd.CommandText = "select * from Log"; + + await using var reader = await selectCmd.ExecuteReaderAsync(); + + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } + } + } +} diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/log4net.config b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/log4net.config new file mode 100644 index 0000000..3360cc6 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore50.Demo/log4net.config @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Log4NetAdoNetAppender.NetCore60.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Log4NetAdoNetAppender.NetCore60.Demo.csproj new file mode 100644 index 0000000..33a0d52 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Log4NetAdoNetAppender.NetCore60.Demo.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + Always + + + diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Program.cs new file mode 100644 index 0000000..bcfc7d7 --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/Program.cs @@ -0,0 +1,113 @@ +using log4net; +using log4net.Config; +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using log4net.Util; +using Microsoft.Data.Sqlite; +using MicroKnights.Log4NetHelper; + +namespace Log4NetAdoNetAppender.NetCore60.Demo +{ + class Program + { + static async Task Main(string[] args) + { + var connectionString = "Data Source=\"./log4net.db\""; + + // Create Log Table + //创建表格 + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + + //Create a table (if it already exists, delete it first) + //创建一个表(如果已经存在,则先删除) + Console.WriteLine("Delete Table Log ......"); + Console.WriteLine("删除日志表 ......"); + await using var delTableCmd = connection.CreateCommand(); + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + Console.WriteLine("Create Table Log ......"); + Console.WriteLine("创建日志表 ......"); + await using var createTableCmd = connection.CreateCommand(); + createTableCmd.CommandText = "CREATE TABLE Log (" + + "Id INTEGER PRIMARY KEY," + + "Date DATETIME NOT NULL," + + "Thread VARCHAR(255) NOT NULL," + + "Level VARCHAR(50) NOT NULL," + + "Logger VARCHAR(255) NOT NULL," + + "Message TEXT DEFAULT NULL," + + "Exception TEXT DEFAULT NULL" + + "); "; + createTableCmd.ExecuteNonQuery(); + } + Console.WriteLine("Create Table Log Success ......"); + // Load configuration + const string Log4netConfigFilename = "log4net.config"; + + if (File.Exists(Log4netConfigFilename) == false) + { + throw new FileNotFoundException($"{Log4netConfigFilename} not found", Log4netConfigFilename); + } + +#if DEBUG + InternalDebugHelper.EnableInternalDebug(delegate (object source, LogReceivedEventArgs eventArgs) + { + Console.WriteLine(eventArgs.LogLog.Message); + if (eventArgs.LogLog.Exception != null) + Console.WriteLine(eventArgs.LogLog.Exception.StackTrace); + }); +#endif + + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo(Log4netConfigFilename)); + + var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + // Analog input some logs + //模拟输入一些日志 + log.Info("This is log information of an analog input info (Info)"); + Console.WriteLine("这是模拟输入信息(info)的日志信息"); + log.Debug("This is log information of an analog input info (Debug)"); + Console.WriteLine("这是模拟输入信息(Debug)的日志信息"); + log.Warn("This is log information of an analog input info (Warn)"); + Console.WriteLine("这是模拟输入信息(Warn)的日志信息"); + log.Error("This is log information of an analog input info (Error)"); + Console.WriteLine("这是模拟输入信息(Error)的日志信息"); + + + // Show logged data from the database + //显示数据库中记录的数据 + await ShowLogDataFromDatabaseWith(connectionString); + + Console.WriteLine("Press Enter to continue"); + Console.WriteLine("按Enter键继续"); + Console.ReadLine(); + } + + private static async Task ShowLogDataFromDatabaseWith(string connectionString) + { + using var connection = new SqliteConnection(connectionString); + + connection.Open(); + + await using var selectCmd = connection.CreateCommand(); + + selectCmd.CommandText = "select * from Log"; + + await using var reader = await selectCmd.ExecuteReaderAsync(); + + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } + } + } +} \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/log4net.config b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/log4net.config new file mode 100644 index 0000000..ea825bb --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.NetCore60.Demo/log4net.config @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 8974bb6597ddd898993b1195a418ea3825c50d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=8B=BE=E7=8E=96?= Date: Sun, 27 Nov 2022 04:24:14 +0800 Subject: [PATCH 2/2] SQLite demo --- .../App.config | 22 +++- .../Log4NetAdoNetAppender.Net46.Demo.csproj | 71 +++++------ .../Program.cs | 46 +++---- .../App.config | 14 ++- .../Log4NetAdoNetAppender.Net48.Demo.csproj | 53 ++++---- .../Program.cs | 112 ++++++++++++++++- .../log4net.config | 113 ++++++++++++++++++ 7 files changed, 343 insertions(+), 88 deletions(-) create mode 100644 demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/log4net.config diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config index 8324aa6..2728a5d 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/App.config @@ -1,6 +1,26 @@ - + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj index 504d390..12ab167 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Log4NetAdoNetAppender.Net46.Demo.csproj @@ -1,5 +1,6 @@  + @@ -37,109 +38,109 @@ - ..\packages\log4net.2.0.15\lib\net45\log4net.dll + ..\..\..\packages\log4net.2.0.15\lib\net45\log4net.dll - ..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetAdoNetAppender.dll + ..\..\..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetAdoNetAppender.dll - ..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetHelper.dll + ..\..\..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard1.3\MicroKnights.Log4NetHelper.dll - ..\packages\Microsoft.Data.Sqlite.1.0.0\lib\net451\Microsoft.Data.Sqlite.dll + ..\..\..\packages\Microsoft.Data.Sqlite.1.0.0\lib\net451\Microsoft.Data.Sqlite.dll - ..\packages\Microsoft.Extensions.Configuration.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll - ..\packages\Microsoft.Extensions.Configuration.Abstractions.1.1.2\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.Abstractions.1.1.2\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll - ..\packages\Microsoft.Extensions.Configuration.FileExtensions.1.1.2\lib\net451\Microsoft.Extensions.Configuration.FileExtensions.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.FileExtensions.1.1.2\lib\net451\Microsoft.Extensions.Configuration.FileExtensions.dll - ..\packages\Microsoft.Extensions.Configuration.Json.1.1.2\lib\net451\Microsoft.Extensions.Configuration.Json.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.Json.1.1.2\lib\net451\Microsoft.Extensions.Configuration.Json.dll - ..\packages\Microsoft.Extensions.FileProviders.Abstractions.1.1.1\lib\netstandard1.0\Microsoft.Extensions.FileProviders.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.FileProviders.Abstractions.1.1.1\lib\netstandard1.0\Microsoft.Extensions.FileProviders.Abstractions.dll - ..\packages\Microsoft.Extensions.FileProviders.Physical.1.1.1\lib\net451\Microsoft.Extensions.FileProviders.Physical.dll + ..\..\..\packages\Microsoft.Extensions.FileProviders.Physical.1.1.1\lib\net451\Microsoft.Extensions.FileProviders.Physical.dll - ..\packages\Microsoft.Extensions.FileSystemGlobbing.1.1.1\lib\net45\Microsoft.Extensions.FileSystemGlobbing.dll + ..\..\..\packages\Microsoft.Extensions.FileSystemGlobbing.1.1.1\lib\net45\Microsoft.Extensions.FileSystemGlobbing.dll - ..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll - ..\packages\Microsoft.Extensions.Primitives.1.1.1\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll + ..\..\..\packages\Microsoft.Extensions.Primitives.1.1.1\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll - ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll + ..\..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + ..\..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - ..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll + ..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + ..\..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - ..\packages\System.Data.Common.4.3.0\lib\net451\System.Data.Common.dll + ..\..\..\packages\System.Data.Common.4.3.0\lib\net451\System.Data.Common.dll - ..\packages\System.Data.SqlClient.4.4.2\lib\net46\System.Data.SqlClient.dll + ..\..\..\packages\System.Data.SqlClient.4.4.2\lib\net46\System.Data.SqlClient.dll - ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + ..\..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - ..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll + ..\..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - ..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + ..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - ..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll + ..\..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + ..\..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + ..\..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - ..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + ..\..\..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll - ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + ..\..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - ..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + ..\..\..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll - ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + ..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll + ..\..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll - ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + ..\..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + ..\..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll - ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll + ..\..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll @@ -148,7 +149,7 @@ - ..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + ..\..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll @@ -170,6 +171,6 @@ 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 - + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs index 0054c09..f36188b 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs +++ b/demo/SQLite/Log4NetAdoNetAppender.Net46.Demo/Program.cs @@ -27,13 +27,19 @@ static async Task Main(string[] args) //创建一个表(如果已经存在,则先删除) Console.WriteLine("Delete Table Log ......"); Console.WriteLine("删除日志表 ......"); - SqliteCommand delTableCmd = connection.CreateCommand(); - delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; - delTableCmd.ExecuteNonQuery(); + + using (var delTableCmd = connection.CreateCommand()) + { + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + } + Console.WriteLine("Create Table Log ......"); Console.WriteLine("创建日志表 ......"); - SqliteCommand createTableCmd = connection.CreateCommand(); - createTableCmd.CommandText = "CREATE TABLE Log (" + + + using (var createTableCmd = connection.CreateCommand()) + { + createTableCmd.CommandText = "CREATE TABLE Log (" + "Id INTEGER PRIMARY KEY," + "Date DATETIME NOT NULL," + "Thread VARCHAR(255) NOT NULL," + @@ -42,7 +48,8 @@ static async Task Main(string[] args) "Message TEXT DEFAULT NULL," + "Exception TEXT DEFAULT NULL" + "); "; - createTableCmd.ExecuteNonQuery(); + createTableCmd.ExecuteNonQuery(); + } } Console.WriteLine("Create Table Log Success ......"); // Load configuration @@ -90,23 +97,20 @@ static async Task Main(string[] args) private static async Task ShowLogDataFromDatabaseWith(string connectionString) { - var connection = new SqliteConnection(connectionString); - - connection.Open(); - - var selectCmd = connection.CreateCommand(); - - selectCmd.CommandText = "select * from Log"; - - var reader = await selectCmd.ExecuteReaderAsync(); - - Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) - .Select(i => reader.GetName(i)))); - - while (await reader.ReadAsync()) + using (var connection = new SqliteConnection(connectionString)) { + connection.Open(); + var selectCmd = connection.CreateCommand(); + selectCmd.CommandText = "select * from Log"; + var reader = await selectCmd.ExecuteReaderAsync(); Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) - .Select(i => reader.GetString(i)))); + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } } } } diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config index 193aecc..16914d3 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/App.config @@ -1,6 +1,18 @@ - + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj index c99c2ce..05c557e 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Log4NetAdoNetAppender.Net48.Demo.csproj @@ -36,77 +36,77 @@ - ..\packages\log4net.2.0.15\lib\net45\log4net.dll + ..\..\..\packages\log4net.2.0.15\lib\net45\log4net.dll - ..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetAdoNetAppender.dll + ..\..\..\packages\MicroKnights.Log4NetAdoNetAppender.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetAdoNetAppender.dll - ..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetHelper.dll + ..\..\..\packages\MicroKnights.Log4NetHelper.2.2.0\lib\netstandard2.0\MicroKnights.Log4NetHelper.dll - ..\packages\Microsoft.Data.Sqlite.Core.3.1.9\lib\netstandard2.0\Microsoft.Data.Sqlite.dll + ..\..\..\packages\Microsoft.Data.Sqlite.Core.3.1.9\lib\netstandard2.0\Microsoft.Data.Sqlite.dll - ..\packages\Microsoft.Extensions.Configuration.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll - ..\packages\Microsoft.Extensions.Configuration.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll - ..\packages\Microsoft.Extensions.Configuration.FileExtensions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.FileExtensions.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.FileExtensions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.FileExtensions.dll - ..\packages\Microsoft.Extensions.Configuration.Json.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Json.dll + ..\..\..\packages\Microsoft.Extensions.Configuration.Json.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Json.dll - ..\packages\Microsoft.Extensions.FileProviders.Abstractions.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.FileProviders.Abstractions.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Abstractions.dll - ..\packages\Microsoft.Extensions.FileProviders.Physical.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Physical.dll + ..\..\..\packages\Microsoft.Extensions.FileProviders.Physical.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Physical.dll - ..\packages\Microsoft.Extensions.FileSystemGlobbing.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileSystemGlobbing.dll + ..\..\..\packages\Microsoft.Extensions.FileSystemGlobbing.2.0.1\lib\netstandard2.0\Microsoft.Extensions.FileSystemGlobbing.dll - ..\packages\Microsoft.Extensions.Logging.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + ..\..\..\packages\Microsoft.Extensions.Logging.Abstractions.2.0.2\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll - ..\packages\Microsoft.Extensions.Primitives.2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll + ..\..\..\packages\Microsoft.Extensions.Primitives.2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll - ..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll + ..\..\..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll - ..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.batteries_v2.dll + ..\..\..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.batteries_v2.dll - ..\packages\SQLitePCLRaw.core.2.0.2\lib\netstandard2.0\SQLitePCLRaw.core.dll + ..\..\..\packages\SQLitePCLRaw.core.2.0.2\lib\netstandard2.0\SQLitePCLRaw.core.dll - ..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.nativelibrary.dll + ..\..\..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.nativelibrary.dll - ..\packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.2\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll + ..\..\..\packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.2\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll - ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll + ..\..\..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll - ..\packages\System.Data.SqlClient.4.4.2\lib\net461\System.Data.SqlClient.dll + ..\..\..\packages\System.Data.SqlClient.4.4.2\lib\net461\System.Data.SqlClient.dll - ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + ..\..\..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll - ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll + ..\..\..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + ..\..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll @@ -122,14 +122,17 @@ + + Always + - + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 - + \ No newline at end of file diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs index 59ef79e..8d59648 100644 --- a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/Program.cs @@ -1,15 +1,117 @@ -using System; -using System.Collections.Generic; +using log4net; +using log4net.Config; +using System; +using System.IO; using System.Linq; -using System.Text; +using System.Reflection; using System.Threading.Tasks; +using log4net.Util; +using Microsoft.Data.Sqlite; +using MicroKnights.Log4NetHelper; namespace Log4NetAdoNetAppender.Net48.Demo { - internal class Program + class Program { - static void Main(string[] args) + static async Task Main(string[] args) { + var connectionString = "Data Source=\"./log4net.db\""; + + // Create Log Table + //创建表格 + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + + //Create a table (if it already exists, delete it first) + //创建一个表(如果已经存在,则先删除) + Console.WriteLine("Delete Table Log ......"); + Console.WriteLine("删除日志表 ......"); + + using (var delTableCmd = connection.CreateCommand()) + { + delTableCmd.CommandText = "DROP TABLE IF EXISTS Log"; + delTableCmd.ExecuteNonQuery(); + } + + Console.WriteLine("Create Table Log ......"); + Console.WriteLine("创建日志表 ......"); + + using (var createTableCmd = connection.CreateCommand()) + { + createTableCmd.CommandText = "CREATE TABLE Log (" + + "Id INTEGER PRIMARY KEY," + + "Date DATETIME NOT NULL," + + "Thread VARCHAR(255) NOT NULL," + + "Level VARCHAR(50) NOT NULL," + + "Logger VARCHAR(255) NOT NULL," + + "Message TEXT DEFAULT NULL," + + "Exception TEXT DEFAULT NULL" + + "); "; + createTableCmd.ExecuteNonQuery(); + } + } + Console.WriteLine("Create Table Log Success ......"); + // Load configuration + const string Log4netConfigFilename = "log4net.config"; + + if (File.Exists(Log4netConfigFilename) == false) + { + throw new FileNotFoundException($"{Log4netConfigFilename} not found", Log4netConfigFilename); + } + +#if DEBUG + InternalDebugHelper.EnableInternalDebug(delegate (object source, LogReceivedEventArgs eventArgs) + { + Console.WriteLine(eventArgs.LogLog.Message); + if (eventArgs.LogLog.Exception != null) + Console.WriteLine(eventArgs.LogLog.Exception.StackTrace); + }); +#endif + + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo(Log4netConfigFilename)); + + var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + // Analog input some logs + //模拟输入一些日志 + log.Info("This is log information of an analog input info (Info)"); + Console.WriteLine("这是模拟输入信息(info)的日志信息"); + log.Debug("This is log information of an analog input info (Debug)"); + Console.WriteLine("这是模拟输入信息(Debug)的日志信息"); + log.Warn("This is log information of an analog input info (Warn)"); + Console.WriteLine("这是模拟输入信息(Warn)的日志信息"); + log.Error("This is log information of an analog input info (Error)"); + Console.WriteLine("这是模拟输入信息(Error)的日志信息"); + + + // Show logged data from the database + //显示数据库中记录的数据 + await ShowLogDataFromDatabaseWith(connectionString); + + Console.WriteLine("Press Enter to continue"); + Console.WriteLine("按Enter键继续"); + Console.ReadLine(); + } + + private static async Task ShowLogDataFromDatabaseWith(string connectionString) + { + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + var selectCmd = connection.CreateCommand(); + selectCmd.CommandText = "select * from Log"; + var reader = await selectCmd.ExecuteReaderAsync(); + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetName(i)))); + + while (await reader.ReadAsync()) + { + Console.WriteLine(string.Join("\t", Enumerable.Range(0, reader.FieldCount) + .Select(i => reader.GetString(i)))); + } + } } } } diff --git a/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/log4net.config b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/log4net.config new file mode 100644 index 0000000..fb9ae8a --- /dev/null +++ b/demo/SQLite/Log4NetAdoNetAppender.Net48.Demo/log4net.config @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +