|
1 |
| -# Blazor.Eetensions.Logger |
2 |
| -Microsoft Extension Logging implementation for ASP.NET Blazor. |
| 1 | +<!---[](https://ci.dot.net/job/dotnet_orleans/job/master/)--> |
| 2 | +[](https://www.nuget.org/packages/Blazor.Extensions.Logging) |
| 3 | +[](https://www.nuget.org/packages/Blazor.Extensions.Logging) |
| 4 | +[](https://github.com/BlazorExtensions/Logging/blob/master/LICENSE) |
3 | 5 |
|
4 |
| -> Note: This is a work in progress. |
| 6 | +# Blazor Extensions |
| 7 | + |
| 8 | +Blazor Extensions are a set of packages with the goal of adding useful things to [Blazor](https://blazor.net). |
| 9 | + |
| 10 | +# Blazor Extensions Logging |
| 11 | + |
| 12 | +This package is a 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 developmer tool console. |
| 16 | + |
| 17 | +# Features |
| 18 | + |
| 19 | +## Content to log |
| 20 | + |
| 21 | +The logger supports the same string formatting what MEL provides, altogether with named parameter replacement in the message. |
| 22 | + |
| 23 | +In addition to that if you like to log an object then in the browser console you'll see an object displayed, and on that you can expand |
| 24 | +members, hierarchies to see what is in them. |
| 25 | + |
| 26 | +If you want to log an enumerable list of objects, then the browser side component will display iy be calling ```console.table```. |
| 27 | + |
| 28 | +## Filtering |
| 29 | + |
| 30 | +The implementation supports the ```ILoggerFactory``` based filtering configuration that is supplied by the Microsoft Extension Logging abstraction. |
| 31 | + |
| 32 | +To keep it lightweight [Microsoft Extensions Configuration](https://github.com/aspnet/Configuration) based configuration is not supported, the logger |
| 33 | +can be only configured in code. |
| 34 | + |
| 35 | +## Log levels |
| 36 | + |
| 37 | +The logger supports the [LogLevels](https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Abstractions/LogLevel.cs) defined in MEL. |
| 38 | + |
| 39 | +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/Initialize.ts#L35). |
| 40 | + |
| 41 | +# Sample configuration |
| 42 | + |
| 43 | +## Setup |
| 44 | + |
| 45 | +The following snippet shows how to setup the browser console logger by registering it for dependency injection in the ```Program.cs``` of the application. |
| 46 | + |
| 47 | +```c# |
| 48 | +var serviceProvider = new BrowserServiceProvider(services => |
| 49 | +{ |
| 50 | + // Add Blazor.Extensions.Logging.BrowserConsoleLogger |
| 51 | + services.AddLogging(builder => builder |
| 52 | + .AddBrowserConsole() // Register the logger with the ILoggerBuilder |
| 53 | + .SetMinimumLevel(LogLevel.Information) // Set the minimum log level to Information |
| 54 | + ); |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +The following snippet shows how to consume the logger in a Blazor component. |
| 61 | + |
| 62 | +```c# |
| 63 | +@inject ILogger<Index> logger |
| 64 | + |
| 65 | +@functions { |
| 66 | + protected override async Task OnInitAsync() |
| 67 | + { |
| 68 | + logger.LogDebug("MyCompoent init"); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +```c# |
| 76 | +[Inject] |
| 77 | +protected ILogger<MyClass> logger; |
| 78 | + |
| 79 | +public void LogSomething() |
| 80 | +{ |
| 81 | + logger.LogDebug("Inside LogSomething"); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +# Contributions and feedback |
| 86 | + |
| 87 | +Please feel free to use the component, open issues, fix bugs or provide feedback. |
| 88 | + |
| 89 | +# Contributors |
| 90 | + |
| 91 | +The following people are the maintainers of the Blazor Extensions projects: |
| 92 | + |
| 93 | +- [Attila Hajdrik](https://github.com/attilah) |
| 94 | +- [Gutemberg Ribiero](https://github.com/galvesribeiro) |
0 commit comments