├── .gitattributes ├── .gitignore ├── Build.ps1 ├── CHANGES.md ├── LICENSE ├── README.md ├── Serilog.Sinks.AzureDocumentDB.sln ├── appveyor.yml ├── assets ├── CommonAssemblyInfo.cs └── Serilog.snk └── src └── Serilog.Sinks.AzureDocumentDb ├── LoggerConfigurationAzureDocumentDbExtensions.cs ├── Serilog.Sinks.AzureDocumentDB.csproj ├── Serilog.snk ├── Sinks ├── AzureDocumentDb │ ├── AzureDocumentDbSink.cs │ └── bulkImport.js ├── Batch │ └── BatchProvider.cs └── Extensions │ └── LogEventExtensions.cs └── global.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | 3 | * text=auto 4 | -------------------------------------------------------------------------------- /.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/* 189 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | 1.5 2 | * Moved from serilog/serilog 3 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serilog.Sinks.AzureDocumentDB 2 | A Serilog sink that writes to Azure DocumentDB. 3 | 4 | ## Getting started 5 | Install [Serilog.Sinks.AzureDocumentDB](https://www.nuget.org/packages/serilog.sinks.azuredocumentdb) from NuGet 6 | 7 | ```PowerShell 8 | Install-Package Serilog.Sinks.AzureDocumentDB 9 | ``` 10 | 11 | Configure logger by calling `WriteTo.AzureDocumentDB(, )` 12 | 13 | ```C# 14 | var logger = new LoggerConfiguration() 15 | .WriteTo.AzureDocumentDB(, ) 16 | .CreateLogger(); 17 | ``` 18 | 19 | ## TTL (Time-to-live) 20 | 21 | Azure DocumentDB is making easier to prune old data with support of Time To Live (TTL) so does Sink. AzureDocumentDB Sink offers TTL at two levels. 22 | 23 | ### Enable TTL at collection level. 24 | 25 | Sink supports TTL at collection level, if collection does not already exist. 26 | 27 | To enable TTL at collection level, set **timeToLive** parameter in code. 28 | 29 | ```C# 30 | .WriteTo.AzureDocumentDB(, , timeToLive: TimeSpan.FromDays(7)) 31 | ``` 32 | If collection in DocumentDB doesn't exists, it will create one and set TTL on collection level causing all logs messages purge older than 7 days. 33 | 34 | 35 | ### Enable TTL at inidividual log message level. 36 | 37 | Sink do support TTL at individual message level. This allows developer to retian log message of high importance longer than of lesser importance. 38 | 39 | ```C# 40 | logger.Information("This message will expire and purge automatically after {@_ttl} seconds", 60); 41 | 42 | logger.Information("Log message will be retained for 30 days {@_ttl}", 2592000); // 30*24*60*60 43 | 44 | logger.Information("Messages of high importance will never expire {@_ttl}", -1); 45 | ``` 46 | 47 | See [TTL behavior](https://azure.microsoft.com/en-us/documentation/articles/documentdb-time-to-live/) in DocumentDB documentation for in depth explianation. 48 | 49 | >Note: `{@_ttl}` is a reserved expression for TTL. 50 | 51 | 52 | 53 | ## XML configuration 54 | 55 | To use the AzureDocumentDB 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: 56 | 57 | ```PowerShell 58 | Install-Package Serilog.Settings.AppSettings 59 | ``` 60 | In your code, call `ReadFrom.AppSettings()` 61 | 62 | ```C# 63 | var logger = new LoggerConfiguration() 64 | .ReadFrom.AppSettings() 65 | .CreateLogger(); 66 | ``` 67 | In your application's App.config or Web.config file, specify the DocumentDB sink assembly and required **endpointUrl** and **authorizationKey** parameters under the `` 68 | 69 | ```XML 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | ``` 79 | 80 | ## Performance 81 | Sink buffers log internally and flush to Azure DocumentDB in batches using dedicated thread. However, it highly depends on type of Azure DocumentDB subscription you have. 82 | 83 | 84 | [![Build status](https://ci.appveyor.com/api/projects/status/schh1be3g817cv3u?svg=true)](https://ci.appveyor.com/project/SaleemMirza/serilog-sinks-azuredocumentdb) -------------------------------------------------------------------------------- /Serilog.Sinks.AzureDocumentDB.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26228.9 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.AzureDocumentDB", "src\Serilog.Sinks.AzureDocumentDb\Serilog.Sinks.AzureDocumentDB.csproj", "{24B7EF95-96DE-4A77-AFA2-23AA81D574D3}" 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 | {24B7EF95-96DE-4A77-AFA2-23AA81D574D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {24B7EF95-96DE-4A77-AFA2-23AA81D574D3}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {24B7EF95-96DE-4A77-AFA2-23AA81D574D3}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {24B7EF95-96DE-4A77-AFA2-23AA81D574D3}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /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: bd9z4P73oltOXudAjPehwp9iDKsPtC+HbgshOrSgoyQKr5xVK+bxJQngrDJkHdY8 20 | skip_symbols: true 21 | on: 22 | branch: /^(master|dev)$/ 23 | -------------------------------------------------------------------------------- /assets/CommonAssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | [assembly: AssemblyVersion("1.0.0.0")] 4 | [assembly: AssemblyFileVersion("1.1.1.1")] 5 | [assembly: AssemblyInformationalVersion("1.0.0")] -------------------------------------------------------------------------------- /assets/Serilog.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serilog-archive/serilog-sinks-azuredocumentdb/27e3d1376da58adb0c970b84ac60c3b349a55637/assets/Serilog.snk -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/LoggerConfigurationAzureDocumentDbExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Serilog Contributors 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 Microsoft.Azure.Documents.Client; 17 | using Serilog.Configuration; 18 | using Serilog.Core; 19 | using Serilog.Events; 20 | using Serilog.Sinks.AzureDocumentDb; 21 | 22 | namespace Serilog 23 | { 24 | /// 25 | /// Adds the WriteTo.AzureDocumentDb() extension method to . 26 | /// 27 | public static class LoggerConfigurationAzureDocumentDBExtensions 28 | { 29 | /// 30 | /// Adds a sink that writes log events to a Azure DocumentDB table in the provided endpoint. 31 | /// 32 | /// The logger configuration. 33 | /// The endpoint URI of the document db. 34 | /// The authorization key of the db. 35 | /// The name of the database to use; will create if it doesn't exist. 36 | /// The name of the collection to use inside the database; will created if it doesn't exist. 37 | /// The minimum log event level required in order to write an event to the sink. 38 | /// Supplies culture-specific formatting information, or null. 39 | /// Store Timestamp in UTC 40 | /// 41 | /// Specifies communication protocol used by driver to communicate with Azure DocumentDB 42 | /// services. 43 | /// 44 | /// 45 | /// The lifespan of documents (roughly 24855 days maximum). Set null to disable document 46 | /// expiration. 47 | /// 48 | /// Maximum number of log entries this sink can hold before stop accepting log messages. Supported size is between 5000 and 25000 49 | /// Number of log messages to be sent as batch. Supported range is between 1 and 1000 50 | /// 51 | /// A switch allowing the pass-through minimum level to be changed at runtime. 52 | /// 53 | /// A required parameter is null. 54 | /// A required parameter value is out of acceptable range. 55 | public static LoggerConfiguration AzureDocumentDB( 56 | this LoggerSinkConfiguration loggerConfiguration, 57 | Uri endpointUri, 58 | string authorizationKey, 59 | string databaseName = "Diagnostics", 60 | string collectionName = "Logs", 61 | LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, 62 | IFormatProvider formatProvider = null, 63 | bool storeTimestampInUtc = true, 64 | Protocol connectionProtocol = Protocol.Https, 65 | TimeSpan? timeToLive = null, 66 | int logBufferSize = 25_000, 67 | int batchSize = 100, 68 | LoggingLevelSwitch levelSwitch = null) 69 | { 70 | if (loggerConfiguration == null) 71 | throw new ArgumentNullException(nameof(loggerConfiguration)); 72 | if (endpointUri == null) 73 | throw new ArgumentNullException(nameof(endpointUri)); 74 | if (authorizationKey == null) 75 | throw new ArgumentNullException(nameof(authorizationKey)); 76 | if ((timeToLive != null) && (timeToLive.Value > TimeSpan.FromDays(24_855))) 77 | throw new ArgumentOutOfRangeException(nameof(timeToLive)); 78 | 79 | return loggerConfiguration.Sink( 80 | new AzureDocumentDBSink( 81 | endpointUri, 82 | authorizationKey, 83 | databaseName, 84 | collectionName, 85 | formatProvider, 86 | storeTimestampInUtc, 87 | connectionProtocol, 88 | timeToLive, 89 | logBufferSize, 90 | batchSize), 91 | restrictedToMinimumLevel, 92 | levelSwitch); 93 | } 94 | 95 | /// 96 | /// Adds a sink that writes log events to a Azure DocumentDB table in the provided endpoint. 97 | /// 98 | /// The logger configuration. 99 | /// The endpoint url of the document db. 100 | /// The authorization key of the db. 101 | /// The name of the database to use; will create if it doesn't exist. 102 | /// The name of the collection to use inside the database; will created if it doesn't exist. 103 | /// The minimum log event level required in order to write an event to the sink. 104 | /// Supplies culture-specific formatting information, or null. 105 | /// Store Timestamp in UTC 106 | /// 107 | /// Specifies communication protocol used by driver to communicate with Azure DocumentDB 108 | /// services. Values can be either https or Tcp 109 | /// 110 | /// The lifespan of documents in seconds. Set null to disable document expiration. 111 | /// Maximum number of log entries this sink can hold before stop accepting log messages. Supported size is between 5000 and 25000 112 | /// Number of log messages to be sent as batch. Supported range is between 1 and 1000 113 | /// 114 | /// A switch allowing the pass-through minimum level to be changed at runtime. 115 | /// 116 | /// A required parameter is null. 117 | /// A required parameter value is out of acceptable range. 118 | public static LoggerConfiguration AzureDocumentDB( 119 | this LoggerSinkConfiguration loggerConfiguration, 120 | string endpointUrl, 121 | string authorizationKey, 122 | string databaseName = "Diagnostics", 123 | string collectionName = "Logs", 124 | LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, 125 | IFormatProvider formatProvider = null, 126 | bool storeTimestampInUtc = false, 127 | string connectionProtocol = "https", 128 | int? timeToLive = null, 129 | int logBufferSize = 25_000, 130 | int batchSize = 100, 131 | LoggingLevelSwitch levelSwitch = null) 132 | { 133 | if (loggerConfiguration == null) 134 | throw new ArgumentNullException(nameof(loggerConfiguration)); 135 | if (string.IsNullOrWhiteSpace(endpointUrl)) 136 | throw new ArgumentNullException(nameof(endpointUrl)); 137 | if (authorizationKey == null) 138 | throw new ArgumentNullException(nameof(authorizationKey)); 139 | if ((timeToLive != null) && (timeToLive.Value > TimeSpan.FromDays(24_855).TotalSeconds)) 140 | throw new ArgumentOutOfRangeException(nameof(timeToLive)); 141 | 142 | TimeSpan? timeSpan = null; 143 | if (timeToLive != null) 144 | timeSpan = TimeSpan.FromSeconds(Math.Max(-1, timeToLive.Value)); 145 | 146 | return loggerConfiguration.Sink( 147 | new AzureDocumentDBSink( 148 | new Uri(endpointUrl), 149 | authorizationKey, 150 | databaseName, 151 | collectionName, 152 | formatProvider, 153 | storeTimestampInUtc, 154 | connectionProtocol?.ToUpper() == "TCP" ? Protocol.Tcp : Protocol.Https, 155 | timeSpan, 156 | logBufferSize, 157 | batchSize), 158 | restrictedToMinimumLevel, 159 | levelSwitch); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/Serilog.Sinks.AzureDocumentDB.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | net45;netstandard1.5;netstandard2.0;netcoreapp2.2 4 | Serilog.Sinks.AzureDocumentDB 5 | Serilog.Sinks.AzureDocumentDB 6 | Serilog Contributors 7 | Serilog 8 | Copyright © Serilog Contributors 2013-2019 9 | Write Serilog events to Azure DocumentDB 10 | True 11 | True 12 | Serilog.snk 13 | 4.5.0.0 14 | 4.5.0 15 | False 16 | serilog;sinks;azure;documentdb 17 | http://serilog.net/images/serilog-sink-nuget.png 18 | https://serilog.net/ 19 | https://github.com/serilog/serilog-sinks-azuredocumentdb 20 | git 21 | True 22 | 23 | 24 | False 25 | 26 | 27 | 28 | $(Version)-$(VersionSuffix) 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/Serilog.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/serilog-archive/serilog-sinks-azuredocumentdb/27e3d1376da58adb0c970b84ac60c3b349a55637/src/Serilog.Sinks.AzureDocumentDb/Serilog.snk -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/Sinks/AzureDocumentDb/AzureDocumentDbSink.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Serilog Contributors 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.Linq; 19 | using System.Net; 20 | using System.Reflection; 21 | using System.Threading; 22 | using System.Threading.Tasks; 23 | using Microsoft.Azure.Documents; 24 | using Microsoft.Azure.Documents.Client; 25 | using Newtonsoft.Json; 26 | using Newtonsoft.Json.Serialization; 27 | using Serilog.Core; 28 | using Serilog.Debugging; 29 | using Serilog.Events; 30 | using Serilog.Sinks.Batch; 31 | using Serilog.Sinks.Extensions; 32 | 33 | namespace Serilog.Sinks.AzureDocumentDb 34 | { 35 | internal class AzureDocumentDBSink : BatchProvider, ILogEventSink 36 | { 37 | private const string BulkStoredProcedureId = "BulkImport"; 38 | private readonly DocumentClient _client; 39 | private readonly IFormatProvider _formatProvider; 40 | private readonly bool _storeTimestampInUtc; 41 | private readonly int? _timeToLive; 42 | private string _bulkStoredProcedureLink; 43 | private DocumentCollection _collection; 44 | private Database _database; 45 | private readonly SemaphoreSlim _semaphoreSlim; 46 | 47 | public AzureDocumentDBSink( 48 | Uri endpointUri, 49 | string authorizationKey, 50 | string databaseName, 51 | string collectionName, 52 | IFormatProvider formatProvider, 53 | bool storeTimestampInUtc, 54 | Protocol connectionProtocol, 55 | TimeSpan? timeToLive, 56 | int logBufferSize = 25_000, 57 | int batchSize = 100) : base(batchSize, logBufferSize) 58 | { 59 | _formatProvider = formatProvider; 60 | 61 | if ((timeToLive != null) && (timeToLive.Value != TimeSpan.MaxValue)) 62 | _timeToLive = (int) timeToLive.Value.TotalSeconds; 63 | 64 | _client = new DocumentClient( 65 | endpointUri, 66 | authorizationKey, 67 | new ConnectionPolicy 68 | { 69 | ConnectionMode = 70 | connectionProtocol == Protocol.Https ? ConnectionMode.Gateway : ConnectionMode.Direct, 71 | ConnectionProtocol = connectionProtocol, 72 | MaxConnectionLimit = Environment.ProcessorCount * 50 + 200 73 | }); 74 | 75 | _storeTimestampInUtc = storeTimestampInUtc; 76 | _semaphoreSlim = new SemaphoreSlim(1, 1); 77 | 78 | CreateDatabaseIfNotExistsAsync(databaseName).Wait(); 79 | CreateCollectionIfNotExistsAsync(collectionName).Wait(); 80 | 81 | JsonConvert.DefaultSettings = () => 82 | { 83 | var settings = new JsonSerializerSettings() 84 | { 85 | ReferenceLoopHandling = ReferenceLoopHandling.Ignore, 86 | ContractResolver = new DefaultContractResolver() 87 | }; 88 | 89 | return settings; 90 | }; 91 | } 92 | 93 | private async Task CreateDatabaseIfNotExistsAsync(string databaseName) 94 | { 95 | SelfLog.WriteLine($"Opening database {databaseName}"); 96 | await _client.OpenAsync().ConfigureAwait(false); 97 | _database = _client.CreateDatabaseQuery().Where(x => x.Id == databaseName).AsEnumerable().FirstOrDefault() 98 | ?? await _client.CreateDatabaseAsync(new Database {Id = databaseName}).ConfigureAwait(false); 99 | } 100 | 101 | private async Task CreateCollectionIfNotExistsAsync(string collectionName) 102 | { 103 | SelfLog.WriteLine($"Creating collection: {collectionName}"); 104 | _collection = _client.CreateDocumentCollectionQuery(_database.SelfLink) 105 | .Where(x => x.Id == collectionName) 106 | .AsEnumerable() 107 | .FirstOrDefault(); 108 | if (_collection == null) { 109 | var documentCollection = new DocumentCollection {Id = collectionName, DefaultTimeToLive = -1}; 110 | _collection = await _client.CreateDocumentCollectionAsync(_database.SelfLink, documentCollection) 111 | .ConfigureAwait(false); 112 | } 113 | 114 | _collectionLink = _collection.SelfLink; 115 | await CreateBulkImportStoredProcedureAsync(_client).ConfigureAwait(false); 116 | } 117 | 118 | private async Task CreateBulkImportStoredProcedureAsync(IDocumentClient client, bool dropExistingProc = false) 119 | { 120 | var currentAssembly = typeof(AzureDocumentDBSink).GetTypeInfo().Assembly; 121 | 122 | SelfLog.WriteLine("Getting required resource."); 123 | var resourceName = currentAssembly.GetManifestResourceNames() 124 | .FirstOrDefault(w => w.EndsWith("bulkImport.js")); 125 | 126 | if (string.IsNullOrEmpty(resourceName)) { 127 | SelfLog.WriteLine("Unable to find required resource."); 128 | 129 | return; 130 | } 131 | 132 | using (var resourceStream = currentAssembly.GetManifestResourceStream(resourceName)) { 133 | if (resourceStream != null) { 134 | var reader = new StreamReader(resourceStream); 135 | var bulkImportSrc = await reader.ReadToEndAsync().ConfigureAwait(false); 136 | try { 137 | var sp = new StoredProcedure {Id = BulkStoredProcedureId, Body = bulkImportSrc}; 138 | 139 | var sproc = GetStoredProcedure(_collectionLink, sp.Id); 140 | 141 | if ((sproc != null) && dropExistingProc) { 142 | await client.DeleteStoredProcedureAsync(sproc.SelfLink).ConfigureAwait(false); 143 | sproc = null; 144 | } 145 | 146 | if (sproc == null) 147 | sproc = await client.CreateStoredProcedureAsync(_collectionLink, sp).ConfigureAwait(false); 148 | 149 | _bulkStoredProcedureLink = sproc.SelfLink; 150 | } 151 | catch (Exception ex) { 152 | SelfLog.WriteLine(ex.Message); 153 | } 154 | } 155 | } 156 | } 157 | 158 | private StoredProcedure GetStoredProcedure(string collectionLink, string id) 159 | { 160 | return _client.CreateStoredProcedureQuery(collectionLink) 161 | .Where(s => s.Id == id) 162 | .AsEnumerable() 163 | .FirstOrDefault(); 164 | } 165 | 166 | #region Parallel Log Processing Support 167 | 168 | protected override async Task WriteLogEventAsync(ICollection logEventsBatch) 169 | { 170 | if (logEventsBatch == null || logEventsBatch.Count == 0) 171 | return true; 172 | 173 | var args = logEventsBatch.Select(x => x.Dictionary(_storeTimestampInUtc, _formatProvider)); 174 | 175 | if ((_timeToLive != null) && (_timeToLive > 0)) 176 | args = args.Select( 177 | x => 178 | { 179 | if (!x.Keys.Contains("ttl")) 180 | x.Add("ttl", _timeToLive); 181 | 182 | return x; 183 | }); 184 | await _semaphoreSlim.WaitAsync().ConfigureAwait(false); 185 | try { 186 | SelfLog.WriteLine($"Sending batch of {logEventsBatch.Count} messages to DocumentDB"); 187 | var storedProcedureResponse = await _client 188 | .ExecuteStoredProcedureAsync(_bulkStoredProcedureLink, args) 189 | .ConfigureAwait(false); 190 | SelfLog.WriteLine(storedProcedureResponse.StatusCode.ToString()); 191 | 192 | return storedProcedureResponse.StatusCode == HttpStatusCode.OK; 193 | } 194 | catch (AggregateException e) { 195 | SelfLog.WriteLine($"ERROR: {(e.InnerException ?? e).Message}"); 196 | 197 | var exception = e.InnerException as DocumentClientException; 198 | if (exception != null) { 199 | if (exception.StatusCode == null) { 200 | var ei = (DocumentClientException) e.InnerException; 201 | if (ei?.StatusCode != null) { 202 | exception = ei; 203 | } 204 | } 205 | } 206 | 207 | if (exception?.StatusCode == null) 208 | return false; 209 | 210 | switch ((int) exception.StatusCode) { 211 | case 429: 212 | var delayTask = Task.Delay(TimeSpan.FromMilliseconds(exception.RetryAfter.Milliseconds + 10)); 213 | delayTask.Wait(); 214 | 215 | break; 216 | default: 217 | await CreateBulkImportStoredProcedureAsync(_client, true).ConfigureAwait(false); 218 | 219 | break; 220 | } 221 | 222 | return false; 223 | } 224 | finally { 225 | _semaphoreSlim.Release(); 226 | } 227 | } 228 | 229 | #endregion 230 | 231 | #region ILogEventSink Support 232 | 233 | private string _collectionLink; 234 | 235 | public void Emit(LogEvent logEvent) 236 | { 237 | if (logEvent != null) { 238 | PushEvent(logEvent); 239 | } 240 | } 241 | 242 | #endregion 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/Sinks/AzureDocumentDb/bulkImport.js: -------------------------------------------------------------------------------- 1 | function bulkImport(docs) { 2 | var collection = getContext().getCollection(); 3 | 4 | // The count of imported docs, also used as current doc index. 5 | var count = 0; 6 | 7 | // Validate input. 8 | if (!docs) throw new Error("The array is undefined or null."); 9 | 10 | var docsLength = docs.length; 11 | if (docsLength == 0) { 12 | getContext().getResponse().setBody(0); 13 | } 14 | 15 | for (var i = 0; i < docsLength; i++) { 16 | var accepted = collection.createDocument(collection.getSelfLink(), 17 | docs[i], 18 | function(err, documentCreated) { 19 | if (!err) { 20 | count++; 21 | } 22 | }); 23 | if (!accepted) break; 24 | } 25 | 26 | getContext().getResponse().setBody(count); 27 | } -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/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 | 35 | private readonly ConcurrentQueue _logEventBatch; 36 | private readonly BlockingCollection> _batchEventsCollection; 37 | private readonly BlockingCollection _eventsCollection; 38 | 39 | private readonly TimeSpan _timerThresholdSpan = TimeSpan.FromSeconds(10); 40 | private readonly TimeSpan _transientThresholdSpan = TimeSpan.FromSeconds(5); 41 | 42 | private readonly Task _timerTask; 43 | private readonly Task _batchTask; 44 | private readonly Task _eventPumpTask; 45 | 46 | private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); 47 | private readonly AutoResetEvent _timerResetEvent = new AutoResetEvent(false); 48 | private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); 49 | 50 | 51 | protected BatchProvider(int batchSize = 100, int maxBufferSize = 25_000) 52 | { 53 | _maxBufferSize = Math.Min(Math.Max(5_000, maxBufferSize), MaxSupportedBufferSize); 54 | _batchSize = Math.Min(Math.Max(batchSize, 1), MaxSupportedBatchSize); 55 | 56 | _logEventBatch = new ConcurrentQueue(); 57 | _batchEventsCollection = new BlockingCollection>(); 58 | _eventsCollection = new BlockingCollection(maxBufferSize); 59 | 60 | _timerTask = Task.Factory.StartNew(TimerPump, TaskCreationOptions.LongRunning); 61 | _eventPumpTask = Task.Factory.StartNew(EventPump, TaskCreationOptions.LongRunning); 62 | 63 | _batchTask = Task.Run( 64 | async () => 65 | { 66 | try { 67 | while (true) { 68 | var logEvents = _batchEventsCollection.Take(_cancellationTokenSource.Token); 69 | SelfLog.WriteLine($"Sending batch of {logEvents.Count} logs"); 70 | 71 | var retValue = await WriteLogEventAsync(logEvents); 72 | if (retValue) { 73 | Interlocked.Add(ref _numMessages, -1 * logEvents.Count); 74 | } 75 | else { 76 | SelfLog.WriteLine($"Retrying after {_transientThresholdSpan.TotalSeconds} seconds..."); 77 | 78 | await Task.Delay(_transientThresholdSpan); 79 | _batchEventsCollection.Add(logEvents); 80 | } 81 | 82 | if (_cancellationTokenSource.IsCancellationRequested) { 83 | _cancellationTokenSource.Token.ThrowIfCancellationRequested(); 84 | } 85 | } 86 | } 87 | catch (OperationCanceledException) { 88 | SelfLog.WriteLine("Shutting down batch processing"); 89 | } 90 | catch (Exception e) { 91 | SelfLog.WriteLine(e.Message); 92 | } 93 | }); 94 | } 95 | 96 | private void TimerPump() 97 | { 98 | while (!_canStop) { 99 | _timerResetEvent.WaitOne(_timerThresholdSpan); 100 | FlushLogEventBatch(); 101 | } 102 | } 103 | 104 | private void EventPump() 105 | { 106 | try { 107 | while (true) { 108 | var logEvent = _eventsCollection.Take(_cancellationTokenSource.Token); 109 | _logEventBatch.Enqueue(logEvent); 110 | 111 | if (_logEventBatch.Count >= _batchSize) { 112 | FlushLogEventBatch(); 113 | } 114 | } 115 | } 116 | catch (OperationCanceledException) { 117 | SelfLog.WriteLine("Shutting down event pump"); 118 | } 119 | catch (Exception e) { 120 | SelfLog.WriteLine(e.Message); 121 | } 122 | } 123 | 124 | private void FlushLogEventBatch() 125 | { 126 | try { 127 | _semaphoreSlim.Wait(_cancellationTokenSource.Token); 128 | 129 | if (!_logEventBatch.Any()) { 130 | return; 131 | } 132 | 133 | var logEventBatchSize = _logEventBatch.Count >= _batchSize ? _batchSize : _logEventBatch.Count; 134 | var logEventList = new List(); 135 | 136 | for (var i = 0; i < logEventBatchSize; i++) { 137 | if (_logEventBatch.TryDequeue(out LogEvent logEvent)) { 138 | logEventList.Add(logEvent); 139 | } 140 | } 141 | 142 | _batchEventsCollection.Add(logEventList); 143 | } 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 | _eventsCollection.Add(logEvent); 157 | Interlocked.Increment(ref _numMessages); 158 | } 159 | 160 | protected abstract Task WriteLogEventAsync(ICollection logEventsBatch); 161 | 162 | #region IDisposable Support 163 | 164 | private bool _disposedValue; // To detect redundant calls 165 | 166 | protected virtual void Dispose(bool disposing) 167 | { 168 | if (!_disposedValue) { 169 | if (disposing) { 170 | FlushAndCloseEventHandlers(); 171 | _semaphoreSlim.Dispose(); 172 | 173 | SelfLog.WriteLine("Sink halted successfully."); 174 | } 175 | 176 | _disposedValue = true; 177 | } 178 | } 179 | 180 | private void FlushAndCloseEventHandlers() 181 | { 182 | try { 183 | SelfLog.WriteLine("Halting sink..."); 184 | 185 | _canStop = true; 186 | _timerResetEvent.Set(); 187 | 188 | // Flush events collection 189 | while (_eventsCollection.TryTake(out LogEvent logEvent)) { 190 | _logEventBatch.Enqueue(logEvent); 191 | 192 | if (_logEventBatch.Count >= _batchSize) { 193 | FlushLogEventBatch(); 194 | } 195 | } 196 | 197 | FlushLogEventBatch(); 198 | 199 | // request cancellation of all tasks 200 | _cancellationTokenSource.Cancel(); 201 | 202 | // Flush events batch 203 | while (_batchEventsCollection.TryTake(out var eventBatch)) { 204 | SelfLog.WriteLine($"Sending batch of {eventBatch.Count} logs"); 205 | WriteLogEventAsync(eventBatch).Wait(TimeSpan.FromSeconds(30)); 206 | } 207 | 208 | Task.WaitAll(new[] {_eventPumpTask, _batchTask, _timerTask}, TimeSpan.FromSeconds(30)); 209 | } 210 | catch (Exception ex) { 211 | SelfLog.WriteLine(ex.Message); 212 | } 213 | } 214 | 215 | public void Dispose() 216 | { 217 | Dispose(true); 218 | } 219 | 220 | #endregion 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/Sinks/Extensions/LogEventExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2016 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 System.Reflection; 20 | using Serilog.Events; 21 | 22 | namespace Serilog.Sinks.Extensions 23 | { 24 | internal static class LogEventExtensions 25 | { 26 | internal static IDictionary Dictionary(this LogEvent logEvent, bool storeTimestampInUtc = false, 27 | IFormatProvider formatProvider = null) 28 | { 29 | return ConvertToDictionary(logEvent, storeTimestampInUtc, formatProvider); 30 | } 31 | 32 | internal static IDictionary Dictionary( 33 | this IReadOnlyDictionary properties) 34 | { 35 | return ConvertToDictionary(properties); 36 | } 37 | 38 | #region Private implementation 39 | 40 | private static dynamic ConvertToDictionary(IReadOnlyDictionary properties) 41 | { 42 | var expObject = new ExpandoObject() as IDictionary; 43 | foreach (var property in properties) 44 | expObject.Add(property.Key, Simplify(property.Value)); 45 | return expObject; 46 | } 47 | 48 | private static dynamic ConvertToDictionary(LogEvent logEvent, bool storeTimestampInUtc, 49 | IFormatProvider formatProvider) 50 | { 51 | var eventObject = new ExpandoObject() as IDictionary; 52 | var messageTemplateText = logEvent.MessageTemplate?.Text ?? string.Empty; 53 | 54 | eventObject.Add("EventIdHash", ComputeMessageTemplateHash(messageTemplateText)); 55 | eventObject.Add("Timestamp", storeTimestampInUtc 56 | ? logEvent.Timestamp.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.fffzzz") 57 | : logEvent.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fffzzz")); 58 | 59 | eventObject.Add("Level", logEvent.Level.ToString()); 60 | eventObject.Add("Message", logEvent.RenderMessage(formatProvider)); 61 | eventObject.Add("MessageTemplate", messageTemplateText); 62 | 63 | var logEventException = logEvent.Exception; 64 | if (logEventException != null) 65 | { 66 | var exceptionObject = new ExpandoObject() as IDictionary; 67 | var exceptionType = logEventException.GetType(); 68 | foreach (var propertyInfo in exceptionType.GetTypeInfo().GetProperties()) 69 | { 70 | if (propertyInfo.Name != "TargetSite") 71 | { 72 | var propertyValue = propertyInfo.GetValue(logEventException, null); 73 | exceptionObject.Add(propertyInfo.Name, propertyValue); 74 | } 75 | } 76 | 77 | eventObject.Add("Exception", exceptionObject); 78 | } 79 | 80 | var eventProperties = logEvent.Properties.Dictionary(); 81 | eventObject.Add("Properties", eventProperties); 82 | 83 | if (!eventProperties.Keys.Contains("_ttl")) 84 | return eventObject; 85 | 86 | 87 | if (!int.TryParse(eventProperties["_ttl"].ToString(), out int ttlValue)) 88 | { 89 | if (TimeSpan.TryParse(eventProperties["_ttl"].ToString(), out TimeSpan ttlTimeSpan)) 90 | { 91 | ttlValue = (int)ttlTimeSpan.TotalSeconds; 92 | } 93 | } 94 | 95 | if (ttlValue <= 0) 96 | ttlValue = -1; 97 | eventObject.Add("ttl", ttlValue); 98 | 99 | return eventObject; 100 | } 101 | 102 | private static object Simplify(LogEventPropertyValue data) 103 | { 104 | var value = data as ScalarValue; 105 | if (value != null) 106 | return value.Value; 107 | 108 | var dictValue = data as DictionaryValue; 109 | if (dictValue != null) 110 | { 111 | var expObject = new ExpandoObject() as IDictionary; 112 | foreach (var item in dictValue.Elements) 113 | { 114 | var key = item.Key.Value as string; 115 | 116 | if(key != null) 117 | expObject.Add(key, Simplify(item.Value)); 118 | } 119 | return expObject; 120 | } 121 | 122 | var seq = data as SequenceValue; 123 | if (seq != null) 124 | return seq.Elements.Select(Simplify).ToArray(); 125 | 126 | var str = data as StructureValue; 127 | if (str == null) return null; 128 | { 129 | try 130 | { 131 | if (str.TypeTag == null) 132 | return str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value)); 133 | 134 | if (!str.TypeTag.StartsWith("DictionaryEntry") && !str.TypeTag.StartsWith("KeyValuePair")) 135 | return str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value)); 136 | 137 | var key = Simplify(str.Properties[0].Value); 138 | if (key == null) 139 | return null; 140 | 141 | var expObject = new ExpandoObject() as IDictionary; 142 | expObject.Add(key.ToString(), Simplify(str.Properties[1].Value)); 143 | return expObject; 144 | } 145 | catch (Exception ex) 146 | { 147 | Console.WriteLine(ex.Message); 148 | } 149 | } 150 | 151 | return null; 152 | } 153 | /// 154 | /// ComputMessageTemplateHash a 32-bit hash of the provided . The 155 | /// resulting hash value can be uses as an event id in lieu of transmitting the 156 | /// full template string. 157 | /// 158 | /// A message template. 159 | /// A 32-bit hash of the template. 160 | private static uint ComputeMessageTemplateHash(string messageTemplate) 161 | { 162 | if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate)); 163 | 164 | // Jenkins one-at-a-time https://en.wikipedia.org/wiki/Jenkins_hash_function 165 | unchecked 166 | { 167 | uint hash = 0; 168 | for (var i = 0; i < messageTemplate.Length; ++i) 169 | { 170 | hash += messageTemplate[i]; 171 | hash += hash << 10; 172 | hash ^= hash >> 6; 173 | } 174 | hash += hash << 3; 175 | hash ^= hash >> 11; 176 | hash += hash << 15; 177 | return hash; 178 | } 179 | } 180 | 181 | #endregion 182 | } 183 | } -------------------------------------------------------------------------------- /src/Serilog.Sinks.AzureDocumentDb/global.json: -------------------------------------------------------------------------------- 1 | { 2 | "projects": ["src", "test"], 3 | "sdk": { 4 | "version": "1.0.1" 5 | } 6 | } --------------------------------------------------------------------------------