├── .editorconfig ├── .github └── workflows │ ├── ci.yml │ └── publish.yml ├── .gitignore ├── Directory.Build.props ├── Directory.Build.targets ├── LICENSE ├── Logging.sln ├── README.md ├── global.json ├── src ├── Blazor.Extensions.Logging │ ├── Blazor.Extensions.Logging.csproj │ ├── BlazorExtensions.png │ ├── BrowserConsoleLogger.cs │ ├── BrowserConsoleLoggerExtensions.cs │ ├── BrowserConsoleLoggerFactoryExtensions.cs │ ├── BrowserConsoleLoggerProvider.cs │ ├── Client │ │ └── BrowserConsoleLogger.ts │ ├── FormattedLogObject.cs │ ├── LogObject.cs │ └── LogObjectType.cs ├── Directory.Build.props └── Directory.Build.targets └── test ├── Blazor.Extensions.Logging.Test ├── App.razor ├── Blazor.Extensions.Logging.Test.csproj ├── Pages │ ├── Index.razor │ └── _Imports.razor ├── Program.cs ├── Shared │ └── MainLayout.razor ├── _Imports.razor └── wwwroot │ ├── index.html │ └── weather.json └── Directory.Build.props /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://EditorConfig.org 2 | 3 | # This file is the top-most EditorConfig file 4 | root = true 5 | 6 | # All Files 7 | [*] 8 | charset = utf-8 9 | end_of_line = crlf 10 | indent_style = space 11 | indent_size = 4 12 | insert_final_newline = false 13 | trim_trailing_whitespace = true 14 | 15 | # Solution Files 16 | [*.sln] 17 | indent_style = tab 18 | 19 | # XML Project Files 20 | [*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}] 21 | indent_size = 2 22 | 23 | # Configuration Files 24 | [*.{json,xml,yml,config,props,targets,nuspec,resx,ruleset,vsixmanifest,vsct}] 25 | indent_size = 2 26 | 27 | # Markdown Files 28 | [*.md] 29 | trim_trailing_whitespace = false 30 | 31 | # Web Files 32 | [*.{htm,html,js,ts,css,scss,less}] 33 | indent_size = 2 34 | insert_final_newline = true 35 | 36 | # Bash Files 37 | [*.sh] 38 | end_of_line = lf 39 | 40 | # Dotnet Code Style Settings 41 | # See https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference 42 | # See http://kent-boogaart.com/blog/editorconfig-reference-for-c-developers 43 | [*.{cs,csx,cake,vb}] 44 | dotnet_sort_system_directives_first = true:warning 45 | dotnet_style_coalesce_expression = true:warning 46 | dotnet_style_collection_initializer = true:warning 47 | dotnet_style_explicit_tuple_names = true:warning 48 | dotnet_style_null_propagation = true:warning 49 | dotnet_style_object_initializer = true:warning 50 | dotnet_style_predefined_type_for_locals_parameters_members = true:warning 51 | dotnet_style_predefined_type_for_member_access = true:warning 52 | dotnet_style_qualification_for_event = true:warning 53 | dotnet_style_qualification_for_field = true:warning 54 | dotnet_style_qualification_for_method = true:warning 55 | dotnet_style_qualification_for_property = true:warning 56 | 57 | # Naming Symbols 58 | # constant_fields - Define constant fields 59 | dotnet_naming_symbols.constant_fields.applicable_kinds = field 60 | dotnet_naming_symbols.constant_fields.required_modifiers = const 61 | # non_private_readonly_fields - Define public, internal and protected readonly fields 62 | dotnet_naming_symbols.non_private_readonly_fields.applicable_accessibilities = public, internal, protected 63 | dotnet_naming_symbols.non_private_readonly_fields.applicable_kinds = field 64 | dotnet_naming_symbols.non_private_readonly_fields.required_modifiers = readonly 65 | # static_readonly_fields - Define static and readonly fields 66 | dotnet_naming_symbols.static_readonly_fields.applicable_kinds = field 67 | dotnet_naming_symbols.static_readonly_fields.required_modifiers = static, readonly 68 | # private_readonly_fields - Define private readonly fields 69 | dotnet_naming_symbols.private_readonly_fields.applicable_accessibilities = private 70 | dotnet_naming_symbols.private_readonly_fields.applicable_kinds = field 71 | dotnet_naming_symbols.private_readonly_fields.required_modifiers = readonly 72 | # public_internal_fields - Define public and internal fields 73 | dotnet_naming_symbols.public_internal_fields.applicable_accessibilities = public, internal 74 | dotnet_naming_symbols.public_internal_fields.applicable_kinds = field 75 | # private_protected_fields - Define private and protected fields 76 | dotnet_naming_symbols.private_protected_fields.applicable_accessibilities = private, protected 77 | dotnet_naming_symbols.private_protected_fields.applicable_kinds = field 78 | # public_symbols - Define any public symbol 79 | dotnet_naming_symbols.public_symbols.applicable_accessibilities = public, internal, protected, protected_internal 80 | dotnet_naming_symbols.public_symbols.applicable_kinds = method, property, event, delegate 81 | # parameters - Defines any parameter 82 | dotnet_naming_symbols.parameters.applicable_kinds = parameter 83 | # non_interface_types - Defines class, struct, enum and delegate types 84 | dotnet_naming_symbols.non_interface_types.applicable_kinds = class, struct, enum, delegate 85 | # interface_types - Defines interfaces 86 | dotnet_naming_symbols.interface_types.applicable_kinds = interface 87 | 88 | # Naming Styles 89 | # camel_case - Define the camelCase style 90 | dotnet_naming_style.camel_case.capitalization = camel_case 91 | # pascal_case - Define the Pascal_case style 92 | dotnet_naming_style.pascal_case.capitalization = pascal_case 93 | # first_upper - The first character must start with an upper-case character 94 | dotnet_naming_style.first_upper.capitalization = first_word_upper 95 | # prefix_interface_interface_with_i - Interfaces must be PascalCase and the first character of an interface must be an 'I' 96 | dotnet_naming_style.prefix_interface_interface_with_i.capitalization = pascal_case 97 | dotnet_naming_style.prefix_interface_interface_with_i.required_prefix = I 98 | 99 | # Naming Rules 100 | # Constant fields must be PascalCase 101 | dotnet_naming_rule.constant_fields_must_be_pascal_case.severity = warning 102 | dotnet_naming_rule.constant_fields_must_be_pascal_case.symbols = constant_fields 103 | dotnet_naming_rule.constant_fields_must_be_pascal_case.style = pascal_case 104 | # Public, internal and protected readonly fields must be PascalCase 105 | dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.severity = warning 106 | dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.symbols = non_private_readonly_fields 107 | dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.style = pascal_case 108 | # Static readonly fields must be PascalCase 109 | dotnet_naming_rule.static_readonly_fields_must_be_pascal_case.severity = warning 110 | dotnet_naming_rule.static_readonly_fields_must_be_pascal_case.symbols = static_readonly_fields 111 | dotnet_naming_rule.static_readonly_fields_must_be_pascal_case.style = pascal_case 112 | # Private readonly fields must be camelCase 113 | dotnet_naming_rule.private_readonly_fields_must_be_camel_case.severity = warning 114 | dotnet_naming_rule.private_readonly_fields_must_be_camel_case.symbols = private_readonly_fields 115 | dotnet_naming_rule.private_readonly_fields_must_be_camel_case.style = camel_case 116 | # Public and internal fields must be PascalCase 117 | dotnet_naming_rule.public_internal_fields_must_be_pascal_case.severity = warning 118 | dotnet_naming_rule.public_internal_fields_must_be_pascal_case.symbols = public_internal_fields 119 | dotnet_naming_rule.public_internal_fields_must_be_pascal_case.style = pascal_case 120 | # Private and protected fields must be camelCase 121 | dotnet_naming_rule.private_protected_fields_must_be_camel_case.severity = warning 122 | dotnet_naming_rule.private_protected_fields_must_be_camel_case.symbols = private_protected_fields 123 | dotnet_naming_rule.private_protected_fields_must_be_camel_case.style = camel_case 124 | # Public members must be capitalized 125 | dotnet_naming_rule.public_members_must_be_capitalized.severity = warning 126 | dotnet_naming_rule.public_members_must_be_capitalized.symbols = public_symbols 127 | dotnet_naming_rule.public_members_must_be_capitalized.style = first_upper 128 | # Parameters must be camelCase 129 | dotnet_naming_rule.parameters_must_be_camel_case.severity = warning 130 | dotnet_naming_rule.parameters_must_be_camel_case.symbols = parameters 131 | dotnet_naming_rule.parameters_must_be_camel_case.style = camel_case 132 | # Class, struct, enum and delegates must be PascalCase 133 | dotnet_naming_rule.non_interface_types_must_be_pascal_case.severity = warning 134 | dotnet_naming_rule.non_interface_types_must_be_pascal_case.symbols = non_interface_types 135 | dotnet_naming_rule.non_interface_types_must_be_pascal_case.style = pascal_case 136 | # Interfaces must be PascalCase and start with an 'I' 137 | dotnet_naming_rule.interface_types_must_be_prefixed_with_i.severity = warning 138 | dotnet_naming_rule.interface_types_must_be_prefixed_with_i.symbols = interface_types 139 | dotnet_naming_rule.interface_types_must_be_prefixed_with_i.style = prefix_interface_interface_with_i 140 | 141 | # C# Code Style Settings 142 | # See https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference 143 | # See http://kent-boogaart.com/blog/editorconfig-reference-for-c-developers 144 | [*.cs,csx,cake] 145 | # Indentation Options 146 | csharp_indent_block_contents = true:warning 147 | csharp_indent_braces = false:warning 148 | csharp_indent_case_contents = true:warning 149 | csharp_indent_labels = no_change:warning 150 | csharp_indent_switch_labels = true:warning 151 | # Style Options 152 | csharp_style_conditional_delegate_call = true:warning 153 | csharp_style_expression_bodied_accessors = true:warning 154 | csharp_style_expression_bodied_constructors = true:warning 155 | csharp_style_expression_bodied_indexers = true:warning 156 | csharp_style_expression_bodied_methods = true:warning 157 | csharp_style_expression_bodied_operators = true:warning 158 | csharp_style_expression_bodied_properties = true:warning 159 | csharp_style_inlined_variable_declaration = true:warning 160 | csharp_style_pattern_matching_over_as_with_null_check = true:warning 161 | csharp_style_pattern_matching_over_is_with_cast_check = true:warning 162 | csharp_style_throw_expression = true:warning 163 | csharp_style_var_elsewhere = true:warning 164 | csharp_style_var_for_built_in_types = true:warning 165 | csharp_style_var_when_type_is_apparent = true:warning 166 | # New Line Options 167 | csharp_new_line_before_catch = true:warning 168 | csharp_new_line_before_else = true:warning 169 | csharp_new_line_before_finally = true:warning 170 | csharp_new_line_before_members_in_anonymous_types = true:warning 171 | csharp_new_line_before_members_in_object_initializers = true:warning 172 | # BUG: Warning level cannot be set https://github.com/dotnet/roslyn/issues/18010 173 | csharp_new_line_before_open_brace = all 174 | csharp_new_line_between_query_expression_clauses = true:warning 175 | # Spacing Options 176 | csharp_space_after_cast = false:warning 177 | csharp_space_after_colon_in_inheritance_clause = true:warning 178 | csharp_space_after_comma = true:warning 179 | csharp_space_after_dot = false:warning 180 | csharp_space_after_keywords_in_control_flow_statements = true:warning 181 | csharp_space_after_semicolon_in_for_statement = true:warning 182 | csharp_space_around_binary_operators = before_and_after:warning 183 | csharp_space_around_declaration_statements = do_not_ignore:warning 184 | csharp_space_before_colon_in_inheritance_clause = true:warning 185 | csharp_space_before_comma = false:warning 186 | csharp_space_before_dot = false:warning 187 | csharp_space_before_semicolon_in_for_statement = false:warning 188 | csharp_space_before_open_square_brackets = false:warning 189 | csharp_space_between_empty_square_brackets = false:warning 190 | csharp_space_between_method_declaration_name_and_open_parenthesis = false:warning 191 | csharp_space_between_method_declaration_parameter_list_parentheses = false:warning 192 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false:warning 193 | csharp_space_between_method_call_name_and_opening_parenthesis = false:warning 194 | csharp_space_between_method_call_parameter_list_parentheses = false:warning 195 | csharp_space_between_method_call_empty_parameter_list_parentheses = false:warning 196 | csharp_space_between_parentheses = expressions:warning 197 | csharp_space_between_square_brackets = false:warning 198 | # Wrapping Options 199 | csharp_preserve_single_line_blocks = true:warning 200 | csharp_preserve_single_line_statements = false:warning -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Setup .NET Core 16 | uses: actions/setup-dotnet@v1 17 | with: 18 | dotnet-version: 5.0.x 19 | - name: Build 20 | run: dotnet build --configuration Release 21 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Setup .NET Core 16 | uses: actions/setup-dotnet@v1 17 | with: 18 | dotnet-version: 5.0.x 19 | - name: Build 20 | run: dotnet build --configuration Release 21 | - name: Pack 22 | working-directory: src/Blazor.Extensions.Logging 23 | run: dotnet pack --configuration Release -p:Version=${GITHUB_REF##*/v} 24 | - name: Push 25 | working-directory: src/Blazor.Extensions.Logging/bin/Release 26 | run: | 27 | dotnet nuget push Blazor.Extensions.Logging.*.nupkg -k ${{ secrets.NUGET_KEY }} -s https://api.nuget.org/v3/index.json 28 | - name: Create Release 29 | uses: actions/create-release@master 30 | env: 31 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 32 | with: 33 | tag_name: ${{ github.ref }} 34 | release_name: Release ${{ github.ref }} 35 | draft: false 36 | prerelease: false 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | [Pp]ublish[Oo]utput/ 21 | x64/ 22 | x86/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | [Ll]og/ 27 | dist/ 28 | 29 | # Visual Studio 2015 cache/options directory 30 | .vs/ 31 | # Uncomment if you have tasks that create the project's static files in wwwroot 32 | wwwroot/ 33 | 34 | # MSTest test Results 35 | [Tt]est[Rr]esult*/ 36 | [Bb]uild[Ll]og.* 37 | 38 | # NUNIT 39 | *.VisualState.xml 40 | TestResult.xml 41 | 42 | # Build Results of an ATL Project 43 | [Dd]ebugPS/ 44 | [Rr]eleasePS/ 45 | dlldata.c 46 | 47 | # .NET Core 48 | project.lock.json 49 | project.fragment.lock.json 50 | artifacts/ 51 | **/Properties/launchSettings.json 52 | 53 | *_i.c 54 | *_p.c 55 | *_i.h 56 | *.ilk 57 | *.meta 58 | *.obj 59 | *.pch 60 | *.pdb 61 | *.pgc 62 | *.pgd 63 | *.rsp 64 | *.sbr 65 | *.tlb 66 | *.tli 67 | *.tlh 68 | *.tmp 69 | *.tmp_proj 70 | *.log 71 | *.vspscc 72 | *.vssscc 73 | .builds 74 | *.pidb 75 | *.svclog 76 | *.scc 77 | 78 | # Chutzpah Test files 79 | _Chutzpah* 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opendb 86 | *.opensdf 87 | *.sdf 88 | *.cachefile 89 | *.VC.db 90 | *.VC.VC.opendb 91 | 92 | # Visual Studio profiler 93 | *.psess 94 | *.vsp 95 | *.vspx 96 | *.sap 97 | 98 | # TFS 2012 Local Workspace 99 | $tf/ 100 | 101 | # Guidance Automation Toolkit 102 | *.gpState 103 | 104 | # ReSharper is a .NET coding add-in 105 | _ReSharper*/ 106 | *.[Rr]e[Ss]harper 107 | *.DotSettings.user 108 | 109 | # JustCode is a .NET coding add-in 110 | .JustCode 111 | 112 | # TeamCity is a build add-in 113 | _TeamCity* 114 | 115 | # DotCover is a Code Coverage Tool 116 | *.dotCover 117 | 118 | # Visual Studio code coverage results 119 | *.coverage 120 | *.coveragexml 121 | 122 | # NCrunch 123 | _NCrunch_* 124 | .*crunch*.local.xml 125 | nCrunchTemp_* 126 | 127 | # MightyMoose 128 | *.mm.* 129 | AutoTest.Net/ 130 | 131 | # Web workbench (sass) 132 | .sass-cache/ 133 | 134 | # Installshield output folder 135 | [Ee]xpress/ 136 | 137 | # DocProject is a documentation generator add-in 138 | DocProject/buildhelp/ 139 | DocProject/Help/*.HxT 140 | DocProject/Help/*.HxC 141 | DocProject/Help/*.hhc 142 | DocProject/Help/*.hhk 143 | DocProject/Help/*.hhp 144 | DocProject/Help/Html2 145 | DocProject/Help/html 146 | 147 | # Click-Once directory 148 | publish/ 149 | 150 | # Publish Web Output 151 | *.[Pp]ublish.xml 152 | *.azurePubxml 153 | # TODO: Comment the next line if you want to checkin your web deploy settings 154 | # but database connection strings (with potential passwords) will be unencrypted 155 | *.pubxml 156 | *.publishproj 157 | 158 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 159 | # checkin your Azure Web App publish settings, but sensitive information contained 160 | # in these scripts will be unencrypted 161 | PublishScripts/ 162 | 163 | # NuGet Packages 164 | *.nupkg 165 | # The packages folder can be ignored because of Package Restore 166 | **/packages/* 167 | # except build/, which is used as an MSBuild target. 168 | !**/packages/build/ 169 | # Uncomment if necessary however generally it will be regenerated when needed 170 | #!**/packages/repositories.config 171 | # NuGet v3's project.json files produces more ignorable files 172 | *.nuget.props 173 | *.nuget.targets 174 | 175 | # Microsoft Azure Build Output 176 | csx/ 177 | *.build.csdef 178 | 179 | # Microsoft Azure Emulator 180 | ecf/ 181 | rcf/ 182 | 183 | # Windows Store app package directories and files 184 | AppPackages/ 185 | BundleArtifacts/ 186 | Package.StoreAssociation.xml 187 | _pkginfo.txt 188 | 189 | # Visual Studio cache files 190 | # files ending in .cache can be ignored 191 | *.[Cc]ache 192 | # but keep track of directories ending in .cache 193 | !*.[Cc]ache/ 194 | 195 | # Others 196 | ClientBin/ 197 | ~$* 198 | *~ 199 | *.dbmdl 200 | *.dbproj.schemaview 201 | *.jfm 202 | *.pfx 203 | *.publishsettings 204 | orleans.codegen.cs 205 | 206 | # Since there are multiple workflows, uncomment next line to ignore bower_components 207 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 208 | #bower_components/ 209 | 210 | # RIA/Silverlight projects 211 | Generated_Code/ 212 | 213 | # Backup & report files from converting an old project file 214 | # to a newer Visual Studio version. Backup files are not needed, 215 | # because we have git ;-) 216 | _UpgradeReport_Files/ 217 | Backup*/ 218 | UpgradeLog*.XML 219 | UpgradeLog*.htm 220 | 221 | # SQL Server files 222 | *.mdf 223 | *.ldf 224 | *.ndf 225 | 226 | # Business Intelligence projects 227 | *.rdl.data 228 | *.bim.layout 229 | *.bim_*.settings 230 | 231 | # Microsoft Fakes 232 | FakesAssemblies/ 233 | 234 | # GhostDoc plugin setting file 235 | *.GhostDoc.xml 236 | 237 | # Node.js Tools for Visual Studio 238 | .ntvs_analysis.dat 239 | node_modules/ 240 | 241 | # Typescript v1 declaration files 242 | typings/ 243 | 244 | # Visual Studio 6 build log 245 | *.plg 246 | 247 | # Visual Studio 6 workspace options file 248 | *.opt 249 | 250 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 251 | *.vbw 252 | 253 | # Visual Studio LightSwitch build output 254 | **/*.HTMLClient/GeneratedArtifacts 255 | **/*.DesktopClient/GeneratedArtifacts 256 | **/*.DesktopClient/ModelManifest.xml 257 | **/*.Server/GeneratedArtifacts 258 | **/*.Server/ModelManifest.xml 259 | _Pvt_Extensions 260 | 261 | # Paket dependency manager 262 | .paket/paket.exe 263 | paket-files/ 264 | 265 | # FAKE - F# Make 266 | .fake/ 267 | 268 | # JetBrains Rider 269 | .idea/ 270 | *.sln.iml 271 | 272 | # CodeRush 273 | .cr/ 274 | 275 | # Python Tools for Visual Studio (PTVS) 276 | __pycache__/ 277 | *.pyc 278 | 279 | # Cake - Uncomment if you are using it 280 | # tools/** 281 | # !tools/packages.config 282 | 283 | # Telerik's JustMock configuration file 284 | *.jmconfig 285 | 286 | # BizTalk build output 287 | *.btp.cs 288 | *.btm.cs 289 | *.odx.cs 290 | *.xsd.cs 291 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Blazor Extensions Contributors 5 | Blazor Extensions 6 | (c) Blazor Extensions Contributors. All rights reserved. 7 | LICENSE 8 | MIT 9 | https://github.com/BlazorExtensions/Logging 10 | BlazorExtensions.png 11 | Microsoft ASP.NET Core Blazor Extensions Logging 12 | 13 | https://github.com/BlazorExtensions/Logging 14 | git 15 | true 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | latest 26 | true 27 | true 28 | 29 | full 30 | 31 | 32 | 33 | net5.0 34 | net5.0 35 | 36 | 37 | 38 | 42 | 43 | 44 | 45 | 49 | 50 | <developer build> 51 | 52 | 53 | 54 | 55 | $(BUILD_SOURCEVERSION) 56 | 57 | 58 | 59 | 60 | $(GIT_COMMIT) 61 | 62 | 63 | 64 | 65 | Not found 66 | $([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory).git')) 67 | $([System.IO.File]::ReadAllText('$(DotGitDir)/HEAD').Trim()) 68 | $(DotGitDir)/$(HeadFileContent.Substring(5)) 69 | $([System.IO.File]::ReadAllText('$(RefPath)').Trim()) 70 | $(HeadFileContent) 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(Version). Commit Hash: $(GitHeadSha) 5 | 6 | 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Blazor Extensions Contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Logging.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 16 3 | VisualStudioVersion = 16.0.28822.285 4 | MinimumVisualStudioVersion = 15.0.26124.0 5 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B286BCBD-DAD8-4DE7-9334-3DE18DF233AF}" 6 | EndProject 7 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{20DAA632-F8AD-4C5F-9E5F-FC82B7CB56A7}" 8 | EndProject 9 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.Logging", "src\Blazor.Extensions.Logging\Blazor.Extensions.Logging.csproj", "{9378C7BF-0899-4835-B8FB-099292C8C63D}" 10 | EndProject 11 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8A34FDEB-D488-4AE3-887B-8254B33C7A13}" 12 | ProjectSection(SolutionItems) = preProject 13 | .vsts-ci.yml = .vsts-ci.yml 14 | .vsts-release.yml = .vsts-release.yml 15 | Directory.Build.props = Directory.Build.props 16 | Directory.Build.targets = Directory.Build.targets 17 | README.md = README.md 18 | EndProjectSection 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.Logging.Test", "test\Blazor.Extensions.Logging.Test\Blazor.Extensions.Logging.Test.csproj", "{C4BB6A39-28E6-454D-8679-92562CEAD0A9}" 21 | EndProject 22 | Global 23 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 24 | Debug|Any CPU = Debug|Any CPU 25 | Debug|x64 = Debug|x64 26 | Debug|x86 = Debug|x86 27 | Release|Any CPU = Release|Any CPU 28 | Release|x64 = Release|x64 29 | Release|x86 = Release|x86 30 | EndGlobalSection 31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 32 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|x64.ActiveCfg = Debug|Any CPU 35 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|x64.Build.0 = Debug|Any CPU 36 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|x86.ActiveCfg = Debug|Any CPU 37 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Debug|x86.Build.0 = Debug|Any CPU 38 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|x64.ActiveCfg = Release|Any CPU 41 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|x64.Build.0 = Release|Any CPU 42 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|x86.ActiveCfg = Release|Any CPU 43 | {9378C7BF-0899-4835-B8FB-099292C8C63D}.Release|x86.Build.0 = Release|Any CPU 44 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|x64.ActiveCfg = Debug|Any CPU 47 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|x64.Build.0 = Debug|Any CPU 48 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|x86.ActiveCfg = Debug|Any CPU 49 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Debug|x86.Build.0 = Debug|Any CPU 50 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|x64.ActiveCfg = Release|Any CPU 53 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|x64.Build.0 = Release|Any CPU 54 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|x86.ActiveCfg = Release|Any CPU 55 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9}.Release|x86.Build.0 = Release|Any CPU 56 | EndGlobalSection 57 | GlobalSection(SolutionProperties) = preSolution 58 | HideSolutionNode = FALSE 59 | EndGlobalSection 60 | GlobalSection(NestedProjects) = preSolution 61 | {9378C7BF-0899-4835-B8FB-099292C8C63D} = {B286BCBD-DAD8-4DE7-9334-3DE18DF233AF} 62 | {C4BB6A39-28E6-454D-8679-92562CEAD0A9} = {20DAA632-F8AD-4C5F-9E5F-FC82B7CB56A7} 63 | EndGlobalSection 64 | GlobalSection(ExtensibilityGlobals) = postSolution 65 | SolutionGuid = {A97C0A4B-E309-4485-BB76-898B37BFBFFF} 66 | EndGlobalSection 67 | EndGlobal 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build](https://github.com/BlazorExtensions/Logging/workflows/CI/badge.svg)](https://github.com/BlazorExtensions/Logging/actions) 2 | [![Package Version](https://img.shields.io/nuget/v/Blazor.Extensions.Logging.svg)](https://www.nuget.org/packages/Blazor.Extensions.Logging) 3 | [![NuGet Downloads](https://img.shields.io/nuget/dt/Blazor.Extensions.Logging.svg)](https://www.nuget.org/packages/Blazor.Extensions.Logging) 4 | [![License](https://img.shields.io/github/license/BlazorExtensions/Logging.svg)](https://github.com/BlazorExtensions/Logging/blob/master/LICENSE) 5 | 6 | # Blazor Extensions 7 | 8 | Blazor Extensions is a set of packages with the goal of adding useful features to [Blazor](https://blazor.net). 9 | 10 | # Blazor Extensions Logging 11 | 12 | This package is an implementation for the [Microsoft Extensions Logging](https://github.com/aspnet/Logging) abstraction to support 13 | using the ```ILogger``` interface in your Blazor code. 14 | 15 | When the component is configured, all the log statements will appear in the browser's developer tools console. 16 | 17 | # Features 18 | 19 | ## Content to log 20 | 21 | The logger supports the same string formatting that MEL provides, together with named parameter replacement in the message. 22 | 23 | Additionaly, you're able to log an object in the browser console. You can expand members and hierachies to see what's contained within. 24 | 25 | If you want to log an enumerable list of objects, then the browser side component will display it by calling ```console.table```. 26 | 27 | ## Filtering 28 | 29 | The implementation supports the ```ILoggerFactory```-based filtering configuration that is supplied by the Microsoft Extension Logging abstraction. 30 | 31 | To keep it lightweight, [Microsoft Extensions Configuration](https://github.com/aspnet/Configuration) based configuration is not supported; the logger can be only configured in code. 32 | 33 | ## Log levels 34 | 35 | The logger supports the [LogLevels](https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Abstractions/LogLevel.cs) defined in MEL. 36 | 37 | Some of the log levels are not available as distinct methods in the browser's developer tool, so the browser side component does some [mapping](https://github.com/BlazorExtensions/Logging/blob/master/src/Blazor.Extensions.Logging.JS/src/BrowserConsoleLogger.ts#L13). 38 | 39 | # Sample configuration 40 | 41 | ## Setup 42 | 43 | The following snippet shows how to setup the browser console logger by registering it for dependency injection in the ```Program.cs``` of the application. 44 | 45 | ```c# 46 | // Add Blazor.Extensions.Logging.BrowserConsoleLogger 47 | builder.Services.AddLogging(builder => builder 48 | .AddBrowserConsole() 49 | .SetMinimumLevel(LogLevel.Trace) 50 | ); 51 | ``` 52 | 53 | ## Usage 54 | 55 | The following snippet shows how to consume the logger in a Blazor component. 56 | 57 | ```c# 58 | @inject ILogger logger 59 | 60 | @functions { 61 | protected override async Task OnInitializedAsync() 62 | { 63 | logger.LogDebug("MyComponent init"); 64 | } 65 | } 66 | ``` 67 | 68 | If you want to consume it outside of a ```cshtml``` based component, then you can use the ```Inject``` attribute to inject it into the class. 69 | 70 | ```c# 71 | [Inject] 72 | protected ILogger Logger {get;set;} 73 | 74 | public void LogSomething() 75 | { 76 | Logger.LogDebug("Inside LogSomething"); 77 | } 78 | ``` 79 | 80 | # Contributions and feedback 81 | 82 | Please feel free to use the component, open issues, fix bugs or provide feedback. 83 | 84 | # Contributors 85 | 86 | The following people are the maintainers of the Blazor Extensions projects: 87 | 88 | - [Attila Hajdrik](https://github.com/attilah) 89 | - [Gutemberg Ribiero](https://github.com/galvesribeiro) 90 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "5.0.103" 4 | } 5 | } -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/Blazor.Extensions.Logging.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Blazor Extensions Logging 7 | Microsoft Extension Logging implementation for ASP.NET Core Blazor. 8 | 9 | 10 | 11 | net5.0 12 | true 13 | Blazor.Extensions.Logging 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Latest 22 | true 23 | true 24 | false 25 | es6 26 | true 27 | true 28 | true 29 | wwwroot 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | all 38 | runtime; build; native; contentfiles; analyzers; buildtransitive 39 | 40 | 41 | 42 | 43 | 44 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/BlazorExtensions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlazorExtensions/Logging/b2ec1846f542e676895a0febbbcfaa4494f070a6/src/Blazor.Extensions.Logging/BlazorExtensions.png -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/BrowserConsoleLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | using Microsoft.Extensions.Logging; 5 | using Microsoft.JSInterop; 6 | 7 | namespace Blazor.Extensions.Logging 8 | { 9 | internal class BrowserConsoleLogger : BrowserConsoleLogger, ILogger 10 | { 11 | public BrowserConsoleLogger (IJSRuntime jsRuntime) : base (jsRuntime, typeof (T).FullName, null) 12 | { 13 | } 14 | 15 | public BrowserConsoleLogger (IJSRuntime jsRuntime, Func filter) : base (jsRuntime, typeof (T).FullName, filter) 16 | { 17 | 18 | } 19 | } 20 | 21 | internal class BrowserConsoleLogger : ILogger, IAsyncDisposable 22 | { 23 | private const string LoggerFunctionName = "log"; 24 | private const string ScriptName = "./_content/Blazor.Extensions.Logging/BrowserConsoleLogger.js"; 25 | 26 | private readonly Lazy> moduleTask; 27 | private IJSObjectReference module; 28 | private Func filter; 29 | 30 | public BrowserConsoleLogger (IJSRuntime jsRuntime, string name, Func filter) 31 | { 32 | this.filter = filter ?? ((category, logLevel) => true); 33 | this.Name = name ?? 34 | throw new ArgumentNullException (nameof (name)); 35 | 36 | this.moduleTask = new (() => jsRuntime.InvokeAsync( 37 | "import", ScriptName).AsTask()); 38 | } 39 | 40 | public async void Log (LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) 41 | { 42 | if (this.module == null) 43 | { 44 | this.module = await moduleTask.Value; 45 | } 46 | 47 | if (!this.IsEnabled (logLevel)) 48 | { 49 | return; 50 | } 51 | 52 | if (formatter == null) 53 | { 54 | throw new ArgumentNullException (nameof (formatter)); 55 | } 56 | 57 | var message = formatter (state, exception); 58 | 59 | if (!(state is FormattedLogObject)) 60 | { 61 | var internalFormatter = new FormattedLogObject (this.Name, logLevel, message, exception); 62 | 63 | message = internalFormatter.ToString (); 64 | } 65 | 66 | await this.module.InvokeAsync (LoggerFunctionName, message); 67 | } 68 | 69 | public bool IsEnabled (LogLevel logLevel) 70 | { 71 | if (logLevel == LogLevel.None) 72 | { 73 | return false; 74 | } 75 | 76 | return this.Filter (this.Name, logLevel); 77 | } 78 | 79 | public Func Filter 80 | { 81 | get => this.filter; 82 | set 83 | { 84 | this.filter = value ?? 85 | throw new ArgumentNullException (nameof (value)); 86 | } 87 | } 88 | 89 | public string Name { get; } 90 | 91 | public IDisposable BeginScope (TState state) => null; 92 | 93 | public async ValueTask DisposeAsync() 94 | { 95 | if (moduleTask.IsValueCreated) 96 | { 97 | var module = await moduleTask.Value; 98 | 99 | await module.DisposeAsync(); 100 | } 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/BrowserConsoleLoggerExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using System; 3 | 4 | namespace Blazor.Extensions.Logging 5 | { 6 | /// 7 | /// ILogger extension methods for common scenarios. 8 | /// 9 | public static partial class BrowserConsoleLoggerExtensions 10 | { 11 | private static readonly Func MessageFormatter = MessageFormatterFunc; 12 | 13 | /// 14 | /// Writes an object as a debug log message. 15 | /// 16 | /// The to write to. 17 | /// The object to log. 18 | /// logger.LogDebug(person) 19 | public static void LogDebug(this ILogger logger, T data) where T : class 20 | { 21 | logger.Log(LogLevel.Debug, data); 22 | } 23 | 24 | /// 25 | /// Writes an object as a debug log message. 26 | /// 27 | /// The to write to. 28 | /// The exception to log. 29 | /// The object to log. 30 | /// logger.LogDebug(exception, person) 31 | public static void LogDebug(this ILogger logger, Exception exception, T data) where T : class 32 | { 33 | logger.Log(LogLevel.Debug, exception, data); 34 | } 35 | 36 | /// 37 | /// Writes an object as a trace log message. 38 | /// 39 | /// The to write to. 40 | /// The exception to log. 41 | /// The object to log. 42 | /// logger.LogTrace(person) 43 | public static void LogTrace(this ILogger logger, T data) where T : class 44 | { 45 | logger.Log(LogLevel.Trace, data); 46 | } 47 | 48 | /// 49 | /// Writes an object as a trace log message. 50 | /// 51 | /// The to write to. 52 | /// The exception to log. 53 | /// The object to log. 54 | /// logger.LogTrace(exception, person) 55 | public static void LogTrace(this ILogger logger, Exception exception, T data) where T : class 56 | { 57 | logger.Log(LogLevel.Trace, exception, data); 58 | } 59 | 60 | 61 | /// 62 | /// Writes an object as an informational log message. 63 | /// 64 | /// The to write to. 65 | /// The exception to log. 66 | /// The object to log. 67 | /// logger.LogInformation(exception, person) 68 | public static void LogInformation(this ILogger logger, Exception exception, T data) where T : class 69 | { 70 | logger.Log(LogLevel.Information, exception, data); 71 | } 72 | 73 | /// 74 | /// Writes an object as an informational log message. 75 | /// 76 | /// The to write to. 77 | /// The object to log. 78 | /// logger.LogInformation(exception, person) 79 | public static void LogInformation(this ILogger logger, T data) where T : class 80 | { 81 | logger.Log(LogLevel.Information, data); 82 | } 83 | 84 | /// 85 | /// Writes an object as a warning log message. 86 | /// 87 | /// The to write to. 88 | /// The object to log. 89 | /// logger.LogWarning(exception, person) 90 | public static void LogWarning(this ILogger logger, T data) where T : class 91 | { 92 | logger.Log(LogLevel.Warning, data); 93 | } 94 | 95 | /// 96 | /// Writes an object as a warning log message. 97 | /// 98 | /// The to write to. 99 | /// The exception to log. 100 | /// The object to log. 101 | /// logger.LogWarning(exception, person) 102 | public static void LogWarning(this ILogger logger, Exception exception, T data) where T : class 103 | { 104 | logger.Log(LogLevel.Warning, exception, data); 105 | } 106 | 107 | /// 108 | /// Writes an object as a error log message. 109 | /// 110 | /// The to write to. 111 | /// The object to log. 112 | /// logger.LogError(exception, person) 113 | public static void LogError(this ILogger logger, T data) where T : class 114 | { 115 | logger.Log(LogLevel.Error, data); 116 | } 117 | 118 | /// 119 | /// Writes an object as a error log message. 120 | /// 121 | /// The to write to. 122 | /// The exception to log. 123 | /// The object to log. 124 | /// logger.LogError(exception, person) 125 | public static void LogError(this ILogger logger, Exception exception, T data) where T : class 126 | { 127 | logger.Log(LogLevel.Error, exception, data); 128 | } 129 | 130 | /// 131 | /// Writes an object as a critical log message. 132 | /// 133 | /// The to write to. 134 | /// The object to log. 135 | /// logger.LogCritical(exception, person) 136 | public static void LogCritical(this ILogger logger, T data) where T : class 137 | { 138 | logger.Log(LogLevel.Critical, data); 139 | } 140 | 141 | /// 142 | /// Writes an object as a critical log message. 143 | /// 144 | /// The to write to. 145 | /// The exception to log. 146 | /// The object to log. 147 | /// logger.LogCritical(exception, person) 148 | public static void LogCritical(this ILogger logger, Exception exception, T data) where T : class 149 | { 150 | logger.Log(LogLevel.Critical, exception, data); 151 | } 152 | 153 | /// 154 | /// Writes an object as a log message at the specified log level. 155 | /// 156 | /// The to write to. 157 | /// Entry will be written on this level. 158 | /// The object to log. 159 | /// logger.Log(LogLevel.Information, exception, person) 160 | public static void Log(this ILogger logger, LogLevel logLevel, T data) where T : class 161 | { 162 | logger.Log(logLevel, null, data); 163 | } 164 | 165 | /// 166 | /// Writes an object as a log message at the specified log level. 167 | /// 168 | /// The to write to. 169 | /// Entry will be written on this level. 170 | /// The exception to log. 171 | /// The object to log. 172 | /// logger.Log(LogLevel.Information, exception, person) 173 | public static void Log(this ILogger logger, LogLevel logLevel, Exception exception, T data) where T : class 174 | { 175 | Func formatter = MessageFormatterFunc; 176 | var consoleLogger = logger as BrowserConsoleLogger; 177 | logger.Log(logLevel, 0, new FormattedLogObject(consoleLogger.Name, logLevel, data, exception), exception, formatter); 178 | } 179 | 180 | private static string MessageFormatterFunc(object state, Exception error) => state.ToString(); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/BrowserConsoleLoggerFactoryExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using Microsoft.Extensions.DependencyInjection.Extensions; 3 | using Microsoft.Extensions.Logging; 4 | using System; 5 | 6 | namespace Blazor.Extensions.Logging 7 | { 8 | public static class BrowserConsoleLoggerFactoryExtensions 9 | { 10 | /// 11 | /// Adds a logger that target the browser's console output 12 | /// The to use. 13 | /// 14 | public static ILoggingBuilder AddBrowserConsole(this ILoggingBuilder builder) 15 | { 16 | builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); 17 | 18 | // HACK: Override the hardcoded ILogger<> injected by Blazor 19 | builder.Services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(BrowserConsoleLogger<>))); 20 | 21 | return builder; 22 | } 23 | 24 | /// 25 | /// Adds a browser console logger that is enabled for .Information or higher. 26 | /// 27 | /// The to use. 28 | public static ILoggerFactory AddBrowserConsole(this ILoggerFactory factory) 29 | { 30 | factory.AddBrowserConsole(LogLevel.Information); 31 | 32 | return factory; 33 | } 34 | 35 | /// 36 | /// Adds a browser console logger that is enabled for s of minLevel or higher. 37 | /// 38 | /// The to use. 39 | /// The minimum to be logged 40 | public static ILoggerFactory AddBrowserConsole(this ILoggerFactory factory, LogLevel minLevel) 41 | { 42 | factory.AddBrowserConsole((category, logLevel) => logLevel >= minLevel); 43 | 44 | return factory; 45 | } 46 | 47 | /// 48 | /// Adds a browser console logger that is enabled as defined by the filter function. 49 | /// 50 | /// The to use. 51 | /// The category filter to apply to logs. 52 | public static ILoggerFactory AddBrowserConsole(this ILoggerFactory factory, Func filter) 53 | { 54 | factory.AddProvider(new BrowserConsoleLoggerProvider(filter)); 55 | 56 | return factory; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/BrowserConsoleLoggerProvider.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using Microsoft.JSInterop; 3 | using System; 4 | using System.Collections.Concurrent; 5 | 6 | namespace Blazor.Extensions.Logging 7 | { 8 | [ProviderAlias("BrowserConsole")] 9 | internal class BrowserConsoleLoggerProvider : ILoggerProvider 10 | { 11 | private static readonly Func TrueFilter = (cat, level) => true; 12 | 13 | private readonly Func filter; 14 | private readonly IJSRuntime runtime; 15 | private ConcurrentDictionary loggers; 16 | 17 | public BrowserConsoleLoggerProvider(IJSRuntime runtime) 18 | { 19 | this.runtime = runtime; 20 | } 21 | 22 | public BrowserConsoleLoggerProvider(Func filter) 23 | { 24 | this.filter = filter ?? throw new ArgumentNullException(nameof(filter)); 25 | } 26 | 27 | public ILogger CreateLogger(string categoryName) 28 | { 29 | if (string.IsNullOrWhiteSpace(categoryName)) 30 | { 31 | throw new ArgumentNullException(nameof(categoryName)); 32 | } 33 | 34 | if (this.loggers == null) 35 | { 36 | this.loggers = new ConcurrentDictionary(); 37 | } 38 | 39 | return this.loggers.GetOrAdd(categoryName, this.CreateLoggerImplementation); 40 | } 41 | 42 | public void Dispose() => this.loggers?.Clear(); 43 | 44 | private BrowserConsoleLogger CreateLoggerImplementation(string name) => new BrowserConsoleLogger(this.runtime, name, this.GetFilter(name)); 45 | 46 | private Func GetFilter(string name) 47 | { 48 | if (this.filter != null) 49 | { 50 | return this.filter; 51 | } 52 | 53 | return TrueFilter; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/Client/BrowserConsoleLogger.ts: -------------------------------------------------------------------------------- 1 | type LogObject = { 2 | category: string; 3 | logLevel: LogLevel; 4 | type: LogObjectType; 5 | payload: unknown; 6 | exception: string; 7 | } 8 | 9 | // enum coming from Microsoft.Extensions.Logging 10 | enum LogLevel { 11 | Trace = 'trace', 12 | Debug = 'debug', 13 | Information = 'information', 14 | Warning = 'warning', 15 | Error = 'error', 16 | Critical = 'critical', 17 | None = 'none' 18 | } 19 | 20 | enum LogObjectType { 21 | String = 'string', 22 | Object = 'object', 23 | Table = 'table' 24 | } 25 | 26 | export const log = (logObjectValue: string): void => { 27 | let logObject: LogObject; 28 | 29 | try { 30 | logObject = JSON.parse(logObjectValue); 31 | } catch (error) { 32 | throw new Error(`Error parsing JSON payload passed to BrowserConsoleLogger: ${error}`); 33 | } 34 | 35 | let logMethod: Function = console.log; 36 | 37 | // if we've a table, we'll print it as a table anyway, it is unlikely that the developer want to log errornous data as a table. 38 | if (logObject.type === LogObjectType.Table) { 39 | logMethod = console.table; 40 | } else { 41 | switch (logObject.logLevel) { 42 | case LogLevel.Trace: 43 | logMethod = console.trace; 44 | break; 45 | case LogLevel.Debug: 46 | logMethod = console.debug; 47 | break; 48 | case LogLevel.Warning: 49 | logMethod = console.warn; 50 | break; 51 | case LogLevel.Error: 52 | case LogLevel.Critical: 53 | logMethod = console.error; 54 | break; 55 | } 56 | } 57 | 58 | if (logObject.type == LogObjectType.Table) { 59 | logMethod(logObject.payload); 60 | } else { 61 | logMethod(`[${logObject.category}]`, logObject.payload); 62 | } 63 | 64 | if (logObject.exception) { 65 | logMethod(`[${logObject.category}] Exception:`, logObject.exception); 66 | } 67 | }; 68 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/FormattedLogObject.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using System; 3 | using System.Collections; 4 | using System.Text.Json; 5 | using System.Text.Json.Serialization; 6 | 7 | namespace Blazor.Extensions.Logging 8 | { 9 | public class FormattedLogObject 10 | { 11 | private static readonly JsonSerializerOptions jsonOptions; 12 | 13 | private readonly LogLevel logLevel; 14 | private readonly object data; 15 | private readonly Exception exception; 16 | private readonly string category; 17 | 18 | static FormattedLogObject() 19 | { 20 | jsonOptions = new JsonSerializerOptions 21 | { 22 | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, 23 | #if DEBUG 24 | WriteIndented = true 25 | #else 26 | WriteIndented = false 27 | #endif 28 | }; 29 | 30 | jsonOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); 31 | } 32 | 33 | public FormattedLogObject (string category, LogLevel logLevel, object data, Exception exception) 34 | { 35 | this.category = category; 36 | this.logLevel = logLevel; 37 | this.data = data; 38 | this.exception = exception; 39 | } 40 | 41 | public override string ToString () 42 | { 43 | if (this.data == null) 44 | { 45 | return string.Empty; 46 | } 47 | 48 | var logObject = default (LogObject); 49 | 50 | if (this.data is string stringData) 51 | { 52 | logObject = new LogObject 53 | { 54 | Category = this.category, 55 | LogLevel = logLevel, 56 | Type = LogObjectType.String, 57 | Payload = stringData, 58 | }; 59 | } 60 | else 61 | { 62 | var isDataEnumerable = this.IsDataEnumerable (this.data); 63 | 64 | logObject = new LogObject 65 | { 66 | Category = this.category, 67 | LogLevel = logLevel, 68 | Type = isDataEnumerable ? LogObjectType.Table : LogObjectType.Object, 69 | Payload = data 70 | }; 71 | } 72 | 73 | if (this.exception != null) 74 | { 75 | logObject.Exception = this.exception.ToString (); 76 | } 77 | 78 | return JsonSerializer.Serialize(logObject, jsonOptions); 79 | } 80 | 81 | private bool IsDataEnumerable (object data) 82 | { 83 | if (data == null || data is string) 84 | { 85 | return false; 86 | } 87 | 88 | if (data as IEnumerable != null) 89 | { 90 | return true; 91 | } 92 | 93 | return false; 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/LogObject.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | 3 | namespace Blazor.Extensions.Logging 4 | { 5 | internal class LogObject 6 | { 7 | public string Category { get; set; } 8 | public LogLevel LogLevel { get; set; } 9 | public LogObjectType Type { get; set; } 10 | public object Payload { get; set; } 11 | public string Exception { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Blazor.Extensions.Logging/LogObjectType.cs: -------------------------------------------------------------------------------- 1 | namespace Blazor.Extensions.Logging 2 | { 3 | // Must be keep in sync with the TypeScript counterpart 4 | internal enum LogObjectType 5 | { 6 | String = 0, 7 | Object = 1, 8 | Table = 2 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | <_ParentDirectoryBuildPropsPath Condition="'$(_DirectoryBuildPropsFile)' != ''">$([System.IO.Path]::Combine('..', '$(_DirectoryBuildPropsFile)')) 4 | 5 | 6 | 7 | 8 | 9 | true 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | <_ParentDirectoryBuildTargetsPath Condition="'$(_DirectoryBuildPropsFile)' != ''">$([System.IO.Path]::Combine('..', '$(_DirectoryBuildTargetsFile)')) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/App.razor: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

Sorry, there's nothing at this address.

8 |
9 |
10 |
11 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/Blazor.Extensions.Logging.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/Pages/Index.razor: -------------------------------------------------------------------------------- 1 | @page "/" 2 | @inject HttpClient Http 3 | @inject ILogger logger 4 | 5 |

Logging Test, Open up your browser devtools to see the logs!

6 | 7 | @functions { 8 | WeatherForecast[] forecasts; 9 | 10 | protected override async Task OnInitializedAsync() 11 | { 12 | forecasts = await Http.GetFromJsonAsync("weather.json"); 13 | 14 | logger.LogTrace("Trace: Forecast"); 15 | logger.LogDebug("Debug: Forecast"); 16 | logger.LogWarning("Warning: Forecast"); 17 | logger.LogError("Error: Forecast"); 18 | logger.LogCritical("Critical: Forecast"); 19 | logger.LogError(new Exception("Oh my, exception"), "Error: Forecast"); 20 | logger.LogInformation(forecasts); 21 | logger.LogInformation(forecasts[0]); 22 | } 23 | 24 | class WeatherForecast 25 | { 26 | public DateTime Date { get; set; } 27 | public int TemperatureC { get; set; } 28 | public string Summary { get; set; } 29 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/Pages/_Imports.razor: -------------------------------------------------------------------------------- 1 | @layout MainLayout 2 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net.Http; 3 | using System.Threading.Tasks; 4 | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | 8 | namespace Blazor.Extensions.Logging.Test 9 | { 10 | public class Program 11 | { 12 | public static async Task Main(string[] args) 13 | { 14 | var builder = WebAssemblyHostBuilder.CreateDefault(args); 15 | 16 | builder.RootComponents.Add("#app"); 17 | 18 | builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); 19 | 20 | // Add Blazor.Extensions.Logging.BrowserConsoleLogger 21 | builder.Services.AddLogging(builder => builder 22 | .AddBrowserConsole() 23 | .SetMinimumLevel(LogLevel.Trace) 24 | ); 25 | 26 | await builder.Build().RunAsync(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/Shared/MainLayout.razor: -------------------------------------------------------------------------------- 1 | @inherits LayoutComponentBase 2 | 3 |
4 | @Body 5 |
6 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/_Imports.razor: -------------------------------------------------------------------------------- 1 | @using System.Net.Http 2 | @using System.Net.Http.Json 3 | @using Microsoft.AspNetCore.Components.Forms 4 | @using Microsoft.AspNetCore.Components.Routing 5 | @using Microsoft.AspNetCore.Components.Web 6 | @using Microsoft.AspNetCore.Components.Web.Virtualization 7 | @using Microsoft.AspNetCore.Components.WebAssembly.Http 8 | @using Microsoft.JSInterop 9 | @using Microsoft.Extensions.Logging 10 | @using Blazor.Extensions.Logging 11 | @using Blazor.Extensions.Logging.Test 12 | @using Blazor.Extensions.Logging.Test.Shared 13 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/wwwroot/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | Blazor.Extensions.Logging.Test 10 | 11 | 31 | 32 | 33 | 34 |
Loading...
35 | 36 |
37 | An unhandled error has occurred. 38 | Reload 39 | 🗙 40 |
41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /test/Blazor.Extensions.Logging.Test/wwwroot/weather.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "date": "2018-05-06", 4 | "temperatureC": 1, 5 | "summary": "Freezing" 6 | }, 7 | { 8 | "date": "2018-05-07", 9 | "temperatureC": 14, 10 | "summary": "Bracing" 11 | }, 12 | { 13 | "date": "2018-05-08", 14 | "temperatureC": -13, 15 | "summary": "Freezing" 16 | }, 17 | { 18 | "date": "2018-05-09", 19 | "temperatureC": -16, 20 | "summary": "Balmy" 21 | }, 22 | { 23 | "date": "2018-05-10", 24 | "temperatureC": -2, 25 | "summary": "Chilly" 26 | } 27 | ] 28 | -------------------------------------------------------------------------------- /test/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | <_ParentDirectoryBuildPropsPath Condition="'$(_DirectoryBuildPropsFile)' != ''">$([System.IO.Path]::Combine('..', '$(_DirectoryBuildPropsFile)')) 4 | 5 | 6 | 7 | 8 | 9 | false 10 | false 11 | $(NoWarn);1591 12 | 13 | 14 | 15 | 16 | $(TestProjectTargetFramework) 17 | 18 | 19 | 20 | 21 | full 22 | 23 | 24 | --------------------------------------------------------------------------------