Microsoft.Extensions.Logging by Microsoft
Nuget / site data
Details
Info
Name: Microsoft.Extensions.Logging
Logging infrastructure.
Author: Microsoft
NuGet: https://www.nuget.org/packages/Microsoft.Extensions.Logging/
You can find more details at https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator-generators/
Source : https://github.com/dotnet/runtime
Original Readme
.NET Runtime
- What is .NET?
- How can I contribute?
- Reporting security issues and security bugs
- Filing issues
- Useful Links
- .NET Foundation
- License
This repo contains the code to build the .NET runtime, libraries and shared host (dotnet
) installers for
all supported platforms, as well as the sources to .NET runtime and libraries.
What is .NET?
Official Starting Page: https://dotnet.microsoft.com
- How to use .NET (with VS, VS Code, command-line CLI)
- Install official releases
- Install daily builds
- Documentation (Get Started, Tutorials, Porting from .NET Framework, API reference, ...)
- Supported OS versions
- Roadmap
- Releases
How can I contribute?
We welcome contributions! Many people all over the world have helped make this project better.
- Contributing explains what kinds of contributions we welcome
- Workflow Instructions explains how to build and test
- Get Up and Running on .NET Core explains how to get nightly builds of the runtime and its libraries to test them in your own projects.
Reporting security issues and security bugs
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter. You can also find these instructions in this repo's Security doc.
Also see info about related Microsoft .NET Core and ASP.NET Core Bug Bounty Program.
Filing issues
This repo should contain issues that are tied to the runtime, the class libraries and frameworks, the installation of the dotnet
binary (sometimes known as the muxer
) and installation of the .NET runtime and libraries.
For other issues, please file them to their appropriate sibling repos. We have links to many of them on our new issue page.
Useful Links
- .NET Core source index / .NET Framework source index
- API Reference docs
- .NET API Catalog (incl. APIs from daily builds and API usage info)
- API docs writing guidelines - useful when writing /// comments
- .NET Discord Server - a place to discuss the development of .NET and its ecosystem
.NET Foundation
.NET Runtime is a .NET Foundation project.
There are many .NET related projects on GitHub.
- .NET home repo - links to 100s of .NET projects, from Microsoft and the community.
- ASP.NET Core home - the best place to start learning about ASP.NET Core.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.
General .NET OSS discussions: .NET Foundation Discussions
License
.NET (including the runtime repo) is licensed under the MIT license.
About
Logging defined and compiled
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- LogDemo.cs
This is the CSharp Project that references Microsoft.Extensions.Logging
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>
</Project>
This is the use of Microsoft.Extensions.Logging in Program.cs
using System.Text.Json;
using Microsoft.Extensions.Logging;
using ILoggerFactory loggerFactory = LoggerFactory.Create(
builder =>
//https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter
builder.AddSimpleConsole()
//builder.AddJsonConsole(
// options =>
// options.JsonWriterOptions = new JsonWriterOptions()
// {
// Indented = true
// })
) ;
ILogger<SampleObject> logger = loggerFactory.CreateLogger<SampleObject>();
logger.LogInformation("test");
//https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator
(new LoggingSample(logger)).TestLogging();
file readonly record struct SampleObject { }
This is the use of Microsoft.Extensions.Logging in LogDemo.cs
using Microsoft.Extensions.Logging;
public partial class LoggingSample
{
private readonly ILogger _logger;
public LoggingSample(ILogger logger)
{
_logger = logger;
}
[LoggerMessage(
EventId = 20,
Level = LogLevel.Critical,
Message = "Value is {value:E}")]
public static partial void UsingFormatSpecifier(
ILogger logger, double value);
[LoggerMessage(
EventId = 9,
Level = LogLevel.Trace,
Message = "Fixed message",
EventName = "CustomEventName")]
public partial void LogWithCustomEventName();
[LoggerMessage(
EventId = 10,
Message = "Welcome to {city} {province}!")]
public partial void LogWithDynamicLogLevel(
string city, LogLevel level, string province);
public void TestLogging()
{
LogWithCustomEventName();
LogWithDynamicLogLevel("Vancouver", LogLevel.Warning, "BC");
LogWithDynamicLogLevel("Vancouver", LogLevel.Information, "BC");
UsingFormatSpecifier(_logger, 12345.6789);
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- LoggerMessage.g.cs
// <auto-generated/>
#nullable enable
partial class LoggingSample
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Double, global::System.Exception?> __UsingFormatSpecifierCallback =
global::Microsoft.Extensions.Logging.LoggerMessage.Define<global::System.Double>(global::Microsoft.Extensions.Logging.LogLevel.Critical, new global::Microsoft.Extensions.Logging.EventId(20, nameof(UsingFormatSpecifier)), "Value is {value:E}", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true });
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
public static partial void UsingFormatSpecifier(global::Microsoft.Extensions.Logging.ILogger logger, global::System.Double value)
{
if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Critical))
{
__UsingFormatSpecifierCallback(logger, value, null);
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __LogWithCustomEventNameCallback =
global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Trace, new global::Microsoft.Extensions.Logging.EventId(9, "CustomEventName"), "Fixed message", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true });
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
public partial void LogWithCustomEventName()
{
if (_logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Trace))
{
__LogWithCustomEventNameCallback(_logger, null);
}
}
/// <summary> This API supports the logging infrastructure and is not intended to be used directly from your code. It is subject to change in the future. </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
private readonly struct __LogWithDynamicLogLevelStruct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
{
private readonly global::System.String _city;
private readonly global::System.String _province;
public __LogWithDynamicLogLevelStruct(global::System.String city, global::System.String province)
{
this._city = city;
this._province = province;
}
public override string ToString()
{
var city = this._city;
var province = this._province;
return $"Welcome to {city} {province}!";
}
public static readonly global::System.Func<__LogWithDynamicLogLevelStruct, global::System.Exception?, string> Format = (state, ex) => state.ToString();
public int Count => 3;
public global::System.Collections.Generic.KeyValuePair<string, object?> this[int index]
{
get => index switch
{
0 => new global::System.Collections.Generic.KeyValuePair<string, object?>("city", this._city),
1 => new global::System.Collections.Generic.KeyValuePair<string, object?>("province", this._province),
2 => new global::System.Collections.Generic.KeyValuePair<string, object?>("{OriginalFormat}", "Welcome to {city} {province}!"),
_ => throw new global::System.IndexOutOfRangeException(nameof(index)), // return the same exception LoggerMessage.Define returns in this case
};
}
public global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, object?>> GetEnumerator()
{
for (int i = 0; i < 3; i++)
{
yield return this[i];
}
}
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
public partial void LogWithDynamicLogLevel(global::System.String city, global::Microsoft.Extensions.Logging.LogLevel level, global::System.String province)
{
if (_logger.IsEnabled(level))
{
_logger.Log(
level,
new global::Microsoft.Extensions.Logging.EventId(10, nameof(LogWithDynamicLogLevel)),
new __LogWithDynamicLogLevelStruct(city, province),
null,
__LogWithDynamicLogLevelStruct.Format);
}
}
}
Usefull
Download Example (.NET C# )
Share Microsoft.Extensions.Logging
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Microsoft.Extensions.Logging