Skip to content

Instantly share code, notes, and snippets.

@maryamariyan
Last active April 29, 2021 18:58
Show Gist options
  • Save maryamariyan/4067efaee75831e3476bffda58f324f5 to your computer and use it in GitHub Desktop.
Save maryamariyan/4067efaee75831e3476bffda58f324f5 to your computer and use it in GitHub Desktop.
Logging Generator Sample
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.4.21216.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0-preview.4.21216.3" />
</ItemGroup>
</Project>
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Abstractions;
using System;
namespace ConsoleApp66
{
class Program
{
static void Main(string[] args)
{
var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger("program");
MyPoco.LogMethod(logger, LogLevel.Critical, "Lyanna", "LA");
}
}
public partial class MyPoco
{
public ILogger logger { get; set; }
[LoggerMessage(EventId = 2, Message = "Hello {name} from {city}")]
public static partial void LogMethod(ILogger logger, LogLevel logLevel, string name, string city);
}
}
@gfoidl
Copy link

gfoidl commented Apr 27, 2021

Should the sample be update to use static methods, because of

warning SYSLIB1009: Logging methods must be static

?

@maryamariyan
Copy link
Author

@gfoidl Updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment