├── .gitattributes ├── src └── Serilog.Sinks.MySQL │ ├── Serilog.snk │ ├── Serilog.Sinks.MySQL.csproj │ ├── LoggerConfigurationMySQLExtensions.cs │ └── Sinks │ ├── Extensions │ └── LogEventExtensions.cs │ ├── MySQL │ └── MySqlSink.cs │ └── Batch │ └── BatchProvider.cs ├── appveyor.yml ├── Build.ps1 ├── serilog-sinks-mysql.sln ├── README.md ├── .gitignore ├── resources └── jetbrains.svg └── LICENSE /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | 3 | * text=auto 4 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/Serilog.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saleem-mirza/serilog-sinks-mysql/HEAD/src/Serilog.Sinks.MySQL/Serilog.snk -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{build}' 2 | skip_tags: true 3 | image: Visual Studio 2017 4 | configuration: Release 5 | install: 6 | - ps: mkdir -Force ".\build\" | Out-Null 7 | - ps: Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile ".\build\installcli.ps1" 8 | - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli" 9 | - ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version latest -Channel 2.0' 10 | - ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path" 11 | build_script: 12 | - ps: ./Build.ps1 13 | test: off 14 | artifacts: 15 | - path: artifacts/Serilog.*.nupkg 16 | deploy: 17 | - provider: NuGet 18 | api_key: 19 | secure: kVO26JLB0/4LwRMAraBZagHoLJPBJxgbYYCEP8PnC0Th74cGwzQijhIqIbl1lmjy 20 | skip_symbols: true 21 | on: 22 | branch: /^(master|dev)$/ 23 | -------------------------------------------------------------------------------- /Build.ps1: -------------------------------------------------------------------------------- 1 | echo "build: Build started" 2 | 3 | Push-Location $PSScriptRoot 4 | 5 | if(Test-Path .\artifacts) { 6 | echo "build: Cleaning .\artifacts" 7 | Remove-Item .\artifacts -Force -Recurse 8 | } 9 | & dotnet restore --no-cache 10 | 11 | $branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL]; 12 | $revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL]; 13 | $suffix = @{ $true = ""; $false = "--version-suffix=$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"] 14 | 15 | echo "build: Version suffix is $suffix" 16 | 17 | & dotnet pack -c Release -o ..\..\artifacts $suffix 18 | 19 | Pop-Location 20 | -------------------------------------------------------------------------------- /serilog-sinks-mysql.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26114.2 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.MySQL", "src\Serilog.Sinks.MySQL\Serilog.Sinks.MySQL.csproj", "{FC74732C-4199-4D17-B89E-76AB37B71BFC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {FC74732C-4199-4D17-B89E-76AB37B71BFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {FC74732C-4199-4D17-B89E-76AB37B71BFC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {FC74732C-4199-4D17-B89E-76AB37B71BFC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {FC74732C-4199-4D17-B89E-76AB37B71BFC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serilog.Sinks.MySQL 2 | Serilog sink that writes to MySQL database. 3 | 4 | ## Getting started 5 | 6 | Install [Serilog.Sinks.MySQL](https://www.nuget.org/packages/Serilog.Sinks.MySQL) from NuGet 7 | 8 | ```PowerShell 9 | Install-Package Serilog.Sinks.MySQL 10 | ``` 11 | 12 | Configure logger by calling WriteTo.MySQL 13 | 14 | ```C# 15 | var logger = new LoggerConfiguration() 16 | .WriteTo.MySQL("server=127.0.0.1;uid=user;pwd=password;database=diagnostics;") 17 | .CreateLogger(); 18 | 19 | logger.Information("This informational message will be written to MySQL database"); 20 | ``` 21 | 22 | ## XML configuration 23 | 24 | To use the rolling file sink with the [Serilog.Settings.AppSettings](https://www.nuget.org/packages/Serilog.Settings.AppSettings) package, first install that package if you haven't already done so: 25 | 26 | ```PowerShell 27 | Install-Package Serilog.Settings.AppSettings 28 | ``` 29 | In your code, call `ReadFrom.AppSettings()` 30 | 31 | ```C# 32 | var logger = new LoggerConfiguration() 33 | .ReadFrom.AppSettings() 34 | .CreateLogger(); 35 | ``` 36 | 37 | In your application's App.config or Web.config file, specify the MySQL sink assembly and required **connectionString** under the `` node: 38 | 39 | ```XML 40 | 41 | 42 | 43 | 44 | 45 | 46 | ``` 47 | 48 | >Note: 49 | This sink version 4.1 has breaking changes. It expects an additional column `Template` of type Template `TEXT` in log table. 50 | It is recommended to add this column manually or delete existing table so that it can be recreated correctly. 51 | 52 | [![Build status](https://ci.appveyor.com/api/projects/status/tse5g3weca5nmky3?svg=true)](https://ci.appveyor.com/project/SaleemMirza/serilog-sinks-mysql) 53 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/Serilog.Sinks.MySQL.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Serilog.Sinks.MySQL 5 | Saleem Mirza 6 | Serilog.Sinks.MySQL 7 | Serilog event sink that writes to MySQL database 8 | Serilog.Sinks.MySQL 9 | serilog;logging;MySQL 10 | http://serilog.net/images/serilog-sink-nuget.png 11 | http://serilog.net 12 | https://github.com/saleem-mirza/serilog-sinks-mysql 13 | git 14 | Copyright © Zethian Inc. 2022-2023 15 | 5.0.0.0 16 | 5.0.0 17 | True 18 | Serilog.snk 19 | netstandard2.0;net7.0 20 | 21 | 22 | LICENSE 23 | 24 | 25 | 26 | true 27 | 28 | 29 | 30 | 31 | $(Version)-$(VersionSuffix) 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/LoggerConfigurationMySQLExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Zethian Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | using System; 16 | using Serilog.Configuration; 17 | using Serilog.Core; 18 | using Serilog.Debugging; 19 | using Serilog.Events; 20 | using Serilog.Sinks.MySQL; 21 | 22 | namespace Serilog 23 | { 24 | /// 25 | /// Adds the WriteTo.MySQL() extension method to . 26 | /// 27 | public static class LoggerConfigurationMySqlExtensions 28 | { 29 | /// 30 | /// Adds a sink that writes log events to a MySQL database. 31 | /// 32 | /// The logger configuration. 33 | /// The connection string to MySQL database. 34 | /// The name of the MySQL table to store log. 35 | /// The minimum log event level required in order to write an event to the sink. 36 | /// Store timestamp in UTC format 37 | /// Number of log messages to be sent as batch. Supported range is between 1 and 1000 38 | /// 39 | /// A switch allowing the pass-through minimum level to be changed at runtime. 40 | /// 41 | /// A required parameter is null. 42 | public static LoggerConfiguration MySQL( 43 | this LoggerSinkConfiguration loggerConfiguration, 44 | string connectionString, 45 | string tableName = "Logs", 46 | LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, 47 | bool storeTimestampInUtc = false, 48 | uint batchSize = 100, 49 | LoggingLevelSwitch levelSwitch = null) 50 | { 51 | if (loggerConfiguration == null) 52 | throw new ArgumentNullException(nameof(loggerConfiguration)); 53 | 54 | if (string.IsNullOrEmpty(connectionString)) 55 | throw new ArgumentNullException(nameof(connectionString)); 56 | 57 | if (batchSize < 1 || batchSize > 1000) 58 | throw new ArgumentOutOfRangeException("[batchSize] argument must be between 1 and 1000 inclusive"); 59 | 60 | try { 61 | return loggerConfiguration.Sink( 62 | new MySqlSink(connectionString, tableName, storeTimestampInUtc, batchSize), 63 | restrictedToMinimumLevel, 64 | levelSwitch); 65 | } 66 | catch (Exception ex) { 67 | SelfLog.WriteLine(ex.Message); 68 | 69 | throw; 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | *.bak 9 | 10 | # Build results 11 | [Dd]ebug/ 12 | [Dd]ebugPublic/ 13 | [Rr]elease/ 14 | [Rr]eleases/ 15 | x64/ 16 | x86/ 17 | build/ 18 | bld/ 19 | [Bb]in/ 20 | [Oo]bj/ 21 | 22 | # Roslyn cache directories 23 | *.ide/ 24 | 25 | # MSTest test Results 26 | [Tt]est[Rr]esult*/ 27 | [Bb]uild[Ll]og.* 28 | 29 | #NUNIT 30 | *.VisualState.xml 31 | TestResult.xml 32 | 33 | # Build Results of an ATL Project 34 | [Dd]ebugPS/ 35 | [Rr]eleasePS/ 36 | dlldata.c 37 | 38 | *_i.c 39 | *_p.c 40 | *_i.h 41 | *.ilk 42 | *.meta 43 | *.obj 44 | *.pch 45 | *.pdb 46 | *.pgc 47 | *.pgd 48 | *.rsp 49 | *.sbr 50 | *.tlb 51 | *.tli 52 | *.tlh 53 | *.tmp 54 | *.tmp_proj 55 | *.log 56 | *.vspscc 57 | *.vssscc 58 | .builds 59 | *.pidb 60 | *.svclog 61 | *.scc 62 | 63 | # Chutzpah Test files 64 | _Chutzpah* 65 | 66 | # Visual C++ cache files 67 | ipch/ 68 | *.aps 69 | *.ncb 70 | *.opensdf 71 | *.sdf 72 | *.cachefile 73 | 74 | # Visual Studio profiler 75 | *.psess 76 | *.vsp 77 | *.vspx 78 | 79 | # TFS 2012 Local Workspace 80 | $tf/ 81 | 82 | # Guidance Automation Toolkit 83 | *.gpState 84 | 85 | # ReSharper is a .NET coding add-in 86 | _ReSharper*/ 87 | *.[Rr]e[Ss]harper 88 | *.DotSettings.user 89 | 90 | # JustCode is a .NET coding addin-in 91 | .JustCode 92 | 93 | # TeamCity is a build add-in 94 | _TeamCity* 95 | 96 | # DotCover is a Code Coverage Tool 97 | *.dotCover 98 | 99 | # NCrunch 100 | _NCrunch_* 101 | .*crunch*.local.xml 102 | 103 | # MightyMoose 104 | *.mm.* 105 | AutoTest.Net/ 106 | 107 | # Web workbench (sass) 108 | .sass-cache/ 109 | 110 | # Installshield output folder 111 | [Ee]xpress/ 112 | 113 | # DocProject is a documentation generator add-in 114 | DocProject/buildhelp/ 115 | DocProject/Help/*.HxT 116 | DocProject/Help/*.HxC 117 | DocProject/Help/*.hhc 118 | DocProject/Help/*.hhk 119 | DocProject/Help/*.hhp 120 | DocProject/Help/Html2 121 | DocProject/Help/html 122 | 123 | # Click-Once directory 124 | publish/ 125 | 126 | # Publish Web Output 127 | *.[Pp]ublish.xml 128 | *.azurePubxml 129 | # TODO: Comment the next line if you want to checkin your web deploy settings 130 | # but database connection strings (with potential passwords) will be unencrypted 131 | *.pubxml 132 | *.publishproj 133 | 134 | # NuGet Packages 135 | *.nupkg 136 | # The packages folder can be ignored because of Package Restore 137 | **/packages/* 138 | # except build/, which is used as an MSBuild target. 139 | !**/packages/build/ 140 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 141 | #!**/packages/repositories.config 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file 167 | # to a newer Visual Studio version. Backup files are not needed, 168 | # because we have git ;-) 169 | _UpgradeReport_Files/ 170 | Backup*/ 171 | UpgradeLog*.XML 172 | UpgradeLog*.htm 173 | 174 | # SQL Server files 175 | *.mdf 176 | *.ldf 177 | 178 | # Business Intelligence projects 179 | *.rdl.data 180 | *.bim.layout 181 | *.bim_*.settings 182 | 183 | # Microsoft Fakes 184 | FakesAssemblies/ 185 | 186 | # Dotnet Core 187 | project.lock.json 188 | /**/.vs/ -------------------------------------------------------------------------------- /resources/jetbrains.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | 34 | 35 | 36 | 39 | 40 | 41 | 42 | 43 | 45 | 47 | 48 | 51 | 54 | 56 | 57 | 59 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/Sinks/Extensions/LogEventExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Zethian Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | using System; 16 | using System.Collections.Generic; 17 | using System.Dynamic; 18 | using System.Linq; 19 | using Newtonsoft.Json; 20 | using Serilog.Events; 21 | 22 | namespace Serilog.Sinks.Extensions 23 | { 24 | internal static class LogEventExtensions 25 | { 26 | internal static string Json(this LogEvent logEvent, bool storeTimestampInUtc = false) 27 | { 28 | return JsonConvert.SerializeObject(ConvertToDictionary(logEvent, storeTimestampInUtc)); 29 | } 30 | 31 | internal static IDictionary Dictionary( 32 | this LogEvent logEvent, 33 | bool storeTimestampInUtc = false, 34 | IFormatProvider formatProvider = null) 35 | { 36 | return ConvertToDictionary(logEvent, storeTimestampInUtc, formatProvider); 37 | } 38 | 39 | internal static string Json(this IReadOnlyDictionary properties) 40 | { 41 | return JsonConvert.SerializeObject(ConvertToDictionary(properties)); 42 | } 43 | 44 | internal static IDictionary Dictionary( 45 | this IReadOnlyDictionary properties) 46 | { 47 | return ConvertToDictionary(properties); 48 | } 49 | 50 | #region Private implementation 51 | 52 | private static dynamic ConvertToDictionary(IReadOnlyDictionary properties) 53 | { 54 | var expObject = new ExpandoObject() as IDictionary; 55 | foreach (var property in properties) 56 | expObject.Add(property.Key, Simplify(property.Value)); 57 | 58 | return expObject; 59 | } 60 | 61 | private static dynamic ConvertToDictionary( 62 | LogEvent logEvent, 63 | bool storeTimestampInUtc, 64 | IFormatProvider formatProvider = null) 65 | { 66 | var eventObject = new ExpandoObject() as IDictionary; 67 | eventObject.Add( 68 | "Timestamp", 69 | storeTimestampInUtc 70 | ? logEvent.Timestamp.ToUniversalTime().ToString("o") 71 | : logEvent.Timestamp.ToString("o")); 72 | 73 | eventObject.Add("LogLevel", logEvent.Level.ToString()); 74 | eventObject.Add("LogMessageTemplate", logEvent.MessageTemplate.Text); 75 | eventObject.Add("LogMessage", logEvent.RenderMessage(formatProvider)); 76 | eventObject.Add("LogException", logEvent.Exception); 77 | eventObject.Add("LogProperties", logEvent.Properties.Dictionary()); 78 | 79 | return eventObject; 80 | } 81 | 82 | private static object Simplify(LogEventPropertyValue data) 83 | { 84 | if (data is ScalarValue value) 85 | return value.Value; 86 | 87 | // ReSharper disable once SuspiciousTypeConversion.Global 88 | if (data is DictionaryValue dictValue) { 89 | var expObject = new ExpandoObject() as IDictionary; 90 | foreach (var item in dictValue.Elements) { 91 | if (item.Key.Value is string key) 92 | expObject.Add(key, Simplify(item.Value)); 93 | } 94 | 95 | return expObject; 96 | } 97 | 98 | if (data is SequenceValue seq) 99 | return seq.Elements.Select(Simplify).ToArray(); 100 | 101 | if (!(data is StructureValue str)) 102 | return null; 103 | 104 | { 105 | try { 106 | if (str.TypeTag == null) 107 | return str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value)); 108 | 109 | if (!str.TypeTag.StartsWith("DictionaryEntry") && !str.TypeTag.StartsWith("KeyValuePair")) 110 | return str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value)); 111 | 112 | var key = Simplify(str.Properties[0].Value); 113 | 114 | if (key == null) 115 | return null; 116 | 117 | var expObject = new ExpandoObject() as IDictionary; 118 | expObject.Add(key.ToString(), Simplify(str.Properties[1].Value)); 119 | 120 | return expObject; 121 | } 122 | catch (Exception ex) { 123 | Console.WriteLine(ex.Message); 124 | } 125 | } 126 | 127 | return null; 128 | } 129 | 130 | #endregion 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/Sinks/MySQL/MySqlSink.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Zethian Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | using System; 16 | using System.Collections.Generic; 17 | using System.IO; 18 | using System.Text; 19 | using System.Threading.Tasks; 20 | using MySql.Data.MySqlClient; 21 | using Serilog.Core; 22 | using Serilog.Debugging; 23 | using Serilog.Events; 24 | using Serilog.Sinks.Batch; 25 | using Serilog.Sinks.Extensions; 26 | 27 | namespace Serilog.Sinks.MySQL 28 | { 29 | internal class MySqlSink : BatchProvider, ILogEventSink 30 | { 31 | private readonly string _connectionString; 32 | private readonly bool _storeTimestampInUtc; 33 | private readonly string _tableName; 34 | 35 | public MySqlSink( 36 | string connectionString, 37 | string tableName = "Logs", 38 | bool storeTimestampInUtc = false, 39 | uint batchSize = 100) : base((int) batchSize) 40 | { 41 | _connectionString = connectionString; 42 | _tableName = tableName; 43 | _storeTimestampInUtc = storeTimestampInUtc; 44 | 45 | var sqlConnection = GetSqlConnection(); 46 | CreateTable(sqlConnection); 47 | } 48 | 49 | public void Emit(LogEvent logEvent) 50 | { 51 | PushEvent(logEvent); 52 | } 53 | 54 | private MySqlConnection GetSqlConnection() 55 | { 56 | try { 57 | var conn = new MySqlConnection(_connectionString); 58 | conn.Open(); 59 | 60 | return conn; 61 | } 62 | catch (Exception ex) { 63 | SelfLog.WriteLine(ex.Message); 64 | 65 | return null; 66 | } 67 | } 68 | 69 | private MySqlCommand GetInsertCommand(MySqlConnection sqlConnection) 70 | { 71 | var tableCommandBuilder = new StringBuilder(); 72 | tableCommandBuilder.Append($"INSERT INTO {_tableName} ("); 73 | tableCommandBuilder.Append("Timestamp, Level, Template, Message, Exception, Properties) "); 74 | tableCommandBuilder.Append("VALUES (@ts, @level,@template, @msg, @ex, @prop)"); 75 | 76 | var cmd = sqlConnection.CreateCommand(); 77 | cmd.CommandText = tableCommandBuilder.ToString(); 78 | 79 | cmd.Parameters.Add(new MySqlParameter("@ts", MySqlDbType.VarChar)); 80 | cmd.Parameters.Add(new MySqlParameter("@level", MySqlDbType.VarChar)); 81 | cmd.Parameters.Add(new MySqlParameter("@template", MySqlDbType.VarChar)); 82 | cmd.Parameters.Add(new MySqlParameter("@msg", MySqlDbType.VarChar)); 83 | cmd.Parameters.Add(new MySqlParameter("@ex", MySqlDbType.VarChar)); 84 | cmd.Parameters.Add(new MySqlParameter("@prop", MySqlDbType.VarChar)); 85 | 86 | return cmd; 87 | } 88 | 89 | private void CreateTable(MySqlConnection sqlConnection) 90 | { 91 | try { 92 | var tableCommandBuilder = new StringBuilder(); 93 | tableCommandBuilder.Append($"CREATE TABLE IF NOT EXISTS {_tableName} ("); 94 | tableCommandBuilder.Append("id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"); 95 | tableCommandBuilder.Append("Timestamp VARCHAR(100),"); 96 | tableCommandBuilder.Append("Level VARCHAR(15),"); 97 | tableCommandBuilder.Append("Template TEXT,"); 98 | tableCommandBuilder.Append("Message TEXT,"); 99 | tableCommandBuilder.Append("Exception TEXT,"); 100 | tableCommandBuilder.Append("Properties TEXT,"); 101 | tableCommandBuilder.Append("_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); 102 | 103 | var cmd = sqlConnection.CreateCommand(); 104 | cmd.CommandText = tableCommandBuilder.ToString(); 105 | cmd.ExecuteNonQuery(); 106 | } 107 | catch (Exception ex) { 108 | SelfLog.WriteLine(ex.Message); 109 | } 110 | } 111 | 112 | protected override async Task WriteLogEventAsync(ICollection logEventsBatch) 113 | { 114 | try { 115 | using (var sqlCon = GetSqlConnection()) { 116 | using (var tr = await sqlCon.BeginTransactionAsync().ConfigureAwait(false)) { 117 | var insertCommand = GetInsertCommand(sqlCon); 118 | insertCommand.Transaction = tr; 119 | 120 | foreach (var logEvent in logEventsBatch) { 121 | var logMessageString = new StringWriter(new StringBuilder()); 122 | logEvent.RenderMessage(logMessageString); 123 | 124 | insertCommand.Parameters["@ts"].Value = _storeTimestampInUtc 125 | ? logEvent.Timestamp.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.fffzzz") 126 | : logEvent.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fffzzz"); 127 | 128 | insertCommand.Parameters["@level"].Value = logEvent.Level.ToString(); 129 | insertCommand.Parameters["@template"].Value = logEvent.MessageTemplate.ToString(); 130 | insertCommand.Parameters["@msg"].Value = logMessageString; 131 | insertCommand.Parameters["@ex"].Value = logEvent.Exception?.ToString(); 132 | insertCommand.Parameters["@prop"].Value = logEvent.Properties.Count > 0 133 | ? logEvent.Properties.Json() 134 | : string.Empty; 135 | 136 | await insertCommand.ExecuteNonQueryAsync().ConfigureAwait(false); 137 | } 138 | 139 | tr.Commit(); 140 | 141 | return true; 142 | } 143 | } 144 | } 145 | catch (Exception ex) { 146 | SelfLog.WriteLine(ex.Message); 147 | 148 | return false; 149 | } 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.MySQL/Sinks/Batch/BatchProvider.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Zethian Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | using Serilog.Debugging; 16 | using Serilog.Events; 17 | using System; 18 | using System.Collections.Concurrent; 19 | using System.Collections.Generic; 20 | using System.Linq; 21 | using System.Threading; 22 | using System.Threading.Tasks; 23 | 24 | namespace Serilog.Sinks.Batch 25 | { 26 | internal abstract class BatchProvider : IDisposable 27 | { 28 | private const int MaxSupportedBufferSize = 100_000; 29 | private const int MaxSupportedBatchSize = 1_000; 30 | private int _numMessages; 31 | private bool _canStop; 32 | private readonly int _maxBufferSize; 33 | private readonly int _batchSize; 34 | private readonly ConcurrentQueue _logEventBatch; 35 | private readonly BlockingCollection> _batchEventsCollection; 36 | private readonly BlockingCollection _eventsCollection; 37 | private readonly TimeSpan _timerThresholdSpan = TimeSpan.FromSeconds(10); 38 | private readonly TimeSpan _transientThresholdSpan = TimeSpan.FromSeconds(5); 39 | private readonly Task _timerTask; 40 | private readonly Task _batchTask; 41 | private readonly Task _eventPumpTask; 42 | private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); 43 | private readonly AutoResetEvent _timerResetEvent = new AutoResetEvent(false); 44 | private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); 45 | 46 | protected BatchProvider(int batchSize = 100, int maxBufferSize = 25_000) 47 | { 48 | _maxBufferSize = Math.Min(Math.Max(5_000, maxBufferSize), MaxSupportedBufferSize); 49 | _batchSize = Math.Min(Math.Max(batchSize, 1), MaxSupportedBatchSize); 50 | 51 | _logEventBatch = new ConcurrentQueue(); 52 | _batchEventsCollection = new BlockingCollection>(); 53 | _eventsCollection = new BlockingCollection(maxBufferSize); 54 | 55 | _batchTask = Task.Factory.StartNew(PumpAsync, TaskCreationOptions.LongRunning); 56 | _timerTask = Task.Factory.StartNew(TimerPump, TaskCreationOptions.LongRunning); 57 | _eventPumpTask = Task.Factory.StartNew(EventPump, TaskCreationOptions.LongRunning); 58 | } 59 | 60 | private async Task PumpAsync() 61 | { 62 | try { 63 | while (!_batchEventsCollection.IsCompleted) { 64 | var logEvents = _batchEventsCollection.Take(_cancellationTokenSource.Token); 65 | SelfLog.WriteLine($"Sending batch of {logEvents.Count} logs"); 66 | 67 | var retValue = await WriteLogEventAsync(logEvents).ConfigureAwait(false); 68 | if (retValue) { 69 | Interlocked.Add(ref _numMessages, -1 * logEvents.Count); 70 | } 71 | else { 72 | SelfLog.WriteLine($"Retrying after {_transientThresholdSpan.TotalSeconds} seconds..."); 73 | 74 | await Task.Delay(_transientThresholdSpan).ConfigureAwait(false); 75 | 76 | if (!_batchEventsCollection.IsAddingCompleted) { 77 | _batchEventsCollection.Add(logEvents); 78 | } 79 | } 80 | 81 | if (_cancellationTokenSource.IsCancellationRequested) { 82 | _cancellationTokenSource.Token.ThrowIfCancellationRequested(); 83 | } 84 | } 85 | } 86 | catch (InvalidOperationException) { } 87 | catch (OperationCanceledException) { } 88 | catch (Exception ex) { 89 | SelfLog.WriteLine(ex.Message); 90 | } 91 | } 92 | 93 | private void TimerPump() 94 | { 95 | while (!_canStop) { 96 | _timerResetEvent.WaitOne(_timerThresholdSpan); 97 | FlushLogEventBatch(); 98 | } 99 | } 100 | 101 | private void EventPump() 102 | { 103 | try { 104 | while (!_eventsCollection.IsCompleted) { 105 | var logEvent = _eventsCollection.Take(_cancellationTokenSource.Token); 106 | _logEventBatch.Enqueue(logEvent); 107 | 108 | if (_logEventBatch.Count >= _batchSize) { 109 | FlushLogEventBatch(); 110 | } 111 | } 112 | } 113 | catch (InvalidOperationException) { } 114 | catch (OperationCanceledException) { } 115 | catch (Exception ex) { 116 | SelfLog.WriteLine(ex.Message); 117 | } 118 | } 119 | 120 | private void FlushLogEventBatch() 121 | { 122 | try { 123 | _semaphoreSlim.Wait(_cancellationTokenSource.Token); 124 | 125 | if (!_logEventBatch.Any()) { 126 | return; 127 | } 128 | 129 | var logEventBatchSize = _logEventBatch.Count >= _batchSize ? _batchSize : _logEventBatch.Count; 130 | var logEventList = new List(); 131 | 132 | for (var i = 0; i < logEventBatchSize; i++) { 133 | if (_logEventBatch.TryDequeue(out LogEvent logEvent)) { 134 | logEventList.Add(logEvent); 135 | } 136 | } 137 | 138 | if (!_batchEventsCollection.IsAddingCompleted) { 139 | _batchEventsCollection.Add(logEventList); 140 | } 141 | } 142 | catch (InvalidOperationException) { } 143 | catch (OperationCanceledException) { } 144 | finally { 145 | if (!_cancellationTokenSource.IsCancellationRequested) { 146 | _semaphoreSlim.Release(); 147 | } 148 | } 149 | } 150 | 151 | protected void PushEvent(LogEvent logEvent) 152 | { 153 | if (_numMessages > _maxBufferSize) 154 | return; 155 | 156 | if (_eventsCollection.IsAddingCompleted) 157 | return; 158 | 159 | _eventsCollection.Add(logEvent); 160 | Interlocked.Increment(ref _numMessages); 161 | } 162 | 163 | protected abstract Task WriteLogEventAsync(ICollection logEventsBatch); 164 | 165 | #region IDisposable Support 166 | 167 | private bool _disposedValue; // To detect redundant calls 168 | 169 | protected virtual void Dispose(bool disposing) 170 | { 171 | if (_disposedValue) 172 | return; 173 | 174 | if (disposing) { 175 | FlushAndCloseEventHandlers(); 176 | _semaphoreSlim.Dispose(); 177 | 178 | SelfLog.WriteLine("Sink halted successfully."); 179 | } 180 | 181 | _disposedValue = true; 182 | } 183 | 184 | private void FlushAndCloseEventHandlers() 185 | { 186 | try { 187 | SelfLog.WriteLine("Halting sink..."); 188 | 189 | _canStop = true; 190 | _timerResetEvent.Set(); 191 | _eventsCollection.CompleteAdding(); 192 | 193 | // Flush events collection 194 | while (!_eventsCollection.IsCompleted) { 195 | var logEvent = _eventsCollection.Take(); 196 | _logEventBatch.Enqueue(logEvent); 197 | if (_logEventBatch.Count >= _batchSize) { 198 | FlushLogEventBatch(); 199 | } 200 | } 201 | 202 | FlushLogEventBatch(); 203 | 204 | _batchEventsCollection.CompleteAdding(); 205 | 206 | // request cancellation of all tasks 207 | _cancellationTokenSource.Cancel(); 208 | 209 | // Flush events batch 210 | while (!_batchEventsCollection.IsCompleted) { 211 | var eventBatch = _batchEventsCollection.Take(); 212 | WriteLogEventAsync(eventBatch).GetAwaiter().GetResult(); 213 | SelfLog.WriteLine($"Sending batch of {eventBatch.Count} logs"); 214 | } 215 | 216 | Task.WaitAll(new[] {_eventPumpTask, _batchTask, _timerTask}, TimeSpan.FromSeconds(60)); 217 | } 218 | catch (Exception ex) { 219 | SelfLog.WriteLine(ex.Message); 220 | } 221 | } 222 | 223 | public void Dispose() 224 | { 225 | Dispose(true); 226 | } 227 | 228 | #endregion 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------