From 484650a097036b1965e485e9e93e1db13a9266d3 Mon Sep 17 00:00:00 2001 From: Allan Targino <13934447+allantargino@users.noreply.github.com> Date: Sat, 7 Jan 2023 13:08:02 -0300 Subject: [PATCH] Adds diagnostics to detect duplicated event names in LSG fixes #64667 --- docs/project/list-of-diagnostics.md | 10 +-- .../gen/DiagnosticDescriptors.cs | 8 +++ .../gen/LoggerMessageGenerator.Parser.cs | 18 ++++-- .../gen/Resources/Strings.resx | 62 ++++++++++--------- .../gen/Resources/xlf/Strings.cs.xlf | 10 +++ .../gen/Resources/xlf/Strings.de.xlf | 10 +++ .../gen/Resources/xlf/Strings.es.xlf | 10 +++ .../gen/Resources/xlf/Strings.fr.xlf | 10 +++ .../gen/Resources/xlf/Strings.it.xlf | 10 +++ .../gen/Resources/xlf/Strings.ja.xlf | 10 +++ .../gen/Resources/xlf/Strings.ko.xlf | 10 +++ .../gen/Resources/xlf/Strings.pl.xlf | 10 +++ .../gen/Resources/xlf/Strings.pt-BR.xlf | 10 +++ .../gen/Resources/xlf/Strings.ru.xlf | 10 +++ .../gen/Resources/xlf/Strings.tr.xlf | 10 +++ .../gen/Resources/xlf/Strings.zh-Hans.xlf | 10 +++ .../gen/Resources/xlf/Strings.zh-Hant.xlf | 10 +++ .../LoggerMessageGeneratorParserTests.cs | 20 ++++++ 18 files changed, 209 insertions(+), 39 deletions(-) diff --git a/docs/project/list-of-diagnostics.md b/docs/project/list-of-diagnostics.md index 01c18c18ee1e3b..6fdd33421b4082 100644 --- a/docs/project/list-of-diagnostics.md +++ b/docs/project/list-of-diagnostics.md @@ -136,11 +136,11 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL | __`SYSLIB1022`__ | Can't have malformed format strings (like dangling {, etc) | | __`SYSLIB1023`__ | Generating more than 6 arguments is not supported | | __`SYSLIB1024`__ | Argument is using the unsupported out parameter modifier | -| __`SYSLIB1025`__ | *_`SYSLIB1024`-`SYSLIB1029` reserved for logging._* | -| __`SYSLIB1026`__ | *_`SYSLIB1024`-`SYSLIB1029` reserved for logging._* | -| __`SYSLIB1027`__ | *_`SYSLIB1024`-`SYSLIB1029` reserved for logging._* | -| __`SYSLIB1028`__ | *_`SYSLIB1024`-`SYSLIB1029` reserved for logging._* | -| __`SYSLIB1029`__ | *_`SYSLIB1024`-`SYSLIB1029` reserved for logging._* | +| __`SYSLIB1025`__ | Multiple logging methods cannot use the same event name within a class | +| __`SYSLIB1026`__ | *_`SYSLIB1026`-`SYSLIB1029` reserved for logging._* | +| __`SYSLIB1027`__ | *_`SYSLIB1026`-`SYSLIB1029` reserved for logging._* | +| __`SYSLIB1028`__ | *_`SYSLIB1026`-`SYSLIB1029` reserved for logging._* | +| __`SYSLIB1029`__ | *_`SYSLIB1026`-`SYSLIB1029` reserved for logging._* | | __`SYSLIB1030`__ | JsonSourceGenerator did not generate serialization metadata for type | | __`SYSLIB1031`__ | JsonSourceGenerator encountered a duplicate JsonTypeInfo property name | | __`SYSLIB1032`__ | JsonSourceGenerator encountered a context class that is not partial | diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs index f9ed87ff2bd4ed..1c732cc5b29b15 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs @@ -191,5 +191,13 @@ public static class DiagnosticDescriptors category: "LoggingGenerator", DiagnosticSeverity.Error, isEnabledByDefault: true); + + public static DiagnosticDescriptor ShouldntReuseEventNames { get; } = new DiagnosticDescriptor( + id: "SYSLIB1025", + title: new LocalizableResourceString(nameof(SR.ShouldntReuseEventNamesTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)), + messageFormat: new LocalizableResourceString(nameof(SR.ShouldntReuseEventNamesMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)), + category: "LoggingGenerator", + DiagnosticSeverity.Warning, + isEnabledByDefault: true); } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs index 9c34e9e9209e4f..bde4ad0e0094ba 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs @@ -68,7 +68,8 @@ public IReadOnlyList GetLogClasses(IEnumerable(); - var ids = new HashSet(); + var eventIds = new HashSet(); + var eventNames = new HashSet(); // we enumerate by syntax tree, to minimize the need to instantiate semantic models (since they're expensive) foreach (var group in classes.GroupBy(x => x.SyntaxTree)) @@ -84,7 +85,10 @@ public IReadOnlyList GetLogClasses(IEnumerable GetLogClasses(IEnumerable - @@ -222,4 +222,10 @@ Argument is using the unsupported out parameter modifier - \ No newline at end of file + + Multiple logging methods are using event name {0} in class {1} + + + Multiple logging methods should not use the same event name within a class + + diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.cs.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.cs.xlf index 67f16f36fd1621..5cb5383e6c017f 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.cs.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.cs.xlf @@ -157,6 +157,16 @@ Více než jedna metoda protokolování nemůže v rámci třídy používat stejné ID události. + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Šablona {0} se neposkytuje jako argument pro metodu protokolování. diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.de.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.de.xlf index a3c44913074226..57531d3d972297 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.de.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.de.xlf @@ -157,6 +157,16 @@ Dieselbe Ereignis-ID kann nicht von mehreren Protokollierungsmethoden innerhalb einer Klasse verwendet werden + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Die Vorlage „{0}“ wird nicht als Argument für die Protokollierungsmethode bereitgestellt diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.es.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.es.xlf index 2e2c1d8de46aa1..f0741cb9161c64 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.es.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.es.xlf @@ -157,6 +157,16 @@ Varios métodos de registro no pueden usar un mismo id. de evento en una clase + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method La plantilla "{0}" no se proporciona como argumento para el método de registro diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.fr.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.fr.xlf index 3ad75b5578f940..ef54bcb26b5420 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.fr.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.fr.xlf @@ -157,6 +157,16 @@ Le même ID d’événement dans une classe ne peut pas être utilisé par plusieurs méthodes de journalisation + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Le modèle « {0} » n’est pas fourni comme argument de la méthode de journalisation diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.it.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.it.xlf index 48bbb69da7347c..dc9403d78700a3 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.it.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.it.xlf @@ -157,6 +157,16 @@ Più metodi di registrazione non possono utilizzare lo stesso ID evento all'interno di una classe + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Il modello ‘{0}’ non è specificato come argomento per il metodo di registrazione diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ja.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ja.xlf index 636bab5a4b603e..2419d64cb983b3 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ja.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ja.xlf @@ -157,6 +157,16 @@ 複数のログ記録方法では、クラス内で同じイベント ID を使用できません + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method テンプレート '{0}' は、ログ メソッドの引数として提供されません diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ko.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ko.xlf index b8d5f257095804..c2c271fd7db239 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ko.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ko.xlf @@ -157,6 +157,16 @@ 여러 로깅 메서드가 한 클래스 내에서 동일한 이벤트 ID를 사용할 수는 없음 + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method 템플릿 {0}이(가) 로깅 메서드에 인수로 제공되지 않음 diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pl.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pl.xlf index 721b1a182a9329..1933bea1f9f45d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pl.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pl.xlf @@ -157,6 +157,16 @@ Wiele metod rejestrowania nie może używać tego samego identyfikatora zdarzenia w obrębie klasy + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Nie podano szablonu „{0}” jako argumentu dla metody rejestrowania diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pt-BR.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pt-BR.xlf index 8aa086a7d078e0..0fcd1d07a80aa7 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pt-BR.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pt-BR.xlf @@ -157,6 +157,16 @@ Múltiplos métodos de registro em log não podem usar o mesmo id de evento dentro de uma classe + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method O modelo '{0}' não é fornecido como argumento para o método de registro diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ru.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ru.xlf index 5739bb98cfc6b4..9da458b6f8b311 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ru.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ru.xlf @@ -157,6 +157,16 @@ Несколько методов ведения журнала не могут использовать одинаковый ИД события в пределах класса + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method Шаблон "{0}" не указан в качестве аргумента для метода ведения журнала diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.tr.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.tr.xlf index 7a02e1b9964492..0e8b2fbd796dc9 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.tr.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.tr.xlf @@ -157,6 +157,16 @@ Birden çok günlüğe kaydetme yöntemi bir sınıf içinde aynı olay kimliğini kullanamaz + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method ‘{0}’ şablonu günlüğe kaydetme yönteminin bağımsız değişkeni olarak sağlanmadı diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hans.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hans.xlf index 7ffa759042e377..da8b1b4bc5e336 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hans.xlf @@ -157,6 +157,16 @@ 多个日志记录方法不能在类中使用相同的事件 ID + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method 未将模板“{0}”作为参数提供给日志记录方法 diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hant.xlf b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hant.xlf index fb85e657f6aca1..3384e527a97fbf 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hant.xlf @@ -157,6 +157,16 @@ 多個記錄方法不能在類別內使用相同的事件識別碼 + + Multiple logging methods are using event name {0} in class {1} + Multiple logging methods are using event name {0} in class {1} + + + + Multiple logging methods should not use the same event name within a class + Multiple logging methods should not use the same event name within a class + + Template '{0}' is not provided as argument to the logging method 未將範本 '{0}' 提供做為記錄方法的引數 diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs index 84fad3acd1704c..564444c32ea2a5 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs @@ -534,6 +534,26 @@ partial class MyClass Assert.Contains("MyClass", diagnostics[0].GetMessage(), StringComparison.InvariantCulture); } + [Fact] + public async Task EventNameReuse() + { + IReadOnlyList diagnostics = await RunGenerator(@" + partial class MyClass + { + [LoggerMessage(EventId = 0, EventName = ""MyEvent"", Level = LogLevel.Debug, Message = ""M1"")] + static partial void M1(ILogger logger); + + [LoggerMessage(EventId = 1, EventName = ""MyEvent"", Level = LogLevel.Debug, Message = ""M1"")] + static partial void M2(ILogger logger); + } + "); + + Assert.Single(diagnostics); + Assert.Equal(DiagnosticDescriptors.ShouldntReuseEventNames.Id, diagnostics[0].Id); + Assert.Contains("MyEvent", diagnostics[0].GetMessage(), StringComparison.InvariantCulture); + Assert.Contains("MyClass", diagnostics[0].GetMessage(), StringComparison.InvariantCulture); + } + [Fact] public async Task MethodReturnType() {