{"id":13431948,"url":"https://github.com/Suchiman/SerilogAnalyzer","last_synced_at":"2025-03-16T22:32:54.019Z","repository":{"id":38866007,"uuid":"59919538","full_name":"Suchiman/SerilogAnalyzer","owner":"Suchiman","description":"Roslyn-based analysis for code using the Serilog logging library. Checks for common mistakes and usage problems.","archived":false,"fork":false,"pushed_at":"2024-04-02T23:17:29.000Z","size":447,"stargazers_count":311,"open_issues_count":23,"forks_count":31,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-14T22:37:36.446Z","etag":null,"topics":["analysis","analyzer","refactor","roslyn","serilog","visual-studio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Suchiman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-29T00:11:51.000Z","updated_at":"2025-02-25T10:52:13.000Z","dependencies_parsed_at":"2024-10-27T11:46:43.151Z","dependency_job_id":"13c52b68-086c-466c-b40f-4baf32f76acb","html_url":"https://github.com/Suchiman/SerilogAnalyzer","commit_stats":{"total_commits":96,"total_committers":8,"mean_commits":12.0,"dds":"0.19791666666666663","last_synced_commit":"bf62860f502db19bc45fd0f46541f383ef3a4455"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suchiman%2FSerilogAnalyzer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suchiman%2FSerilogAnalyzer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suchiman%2FSerilogAnalyzer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Suchiman%2FSerilogAnalyzer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Suchiman","download_url":"https://codeload.github.com/Suchiman/SerilogAnalyzer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658127,"owners_count":20326461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analysis","analyzer","refactor","roslyn","serilog","visual-studio"],"created_at":"2024-07-31T02:01:07.275Z","updated_at":"2025-03-16T22:32:54.010Z","avatar_url":"https://github.com/Suchiman.png","language":"C#","readme":"# SerilogAnalyzer\n\nRoslyn-based analysis for code using the [Serilog](http://serilog.net) logging library. Checks for common mistakes and usage problems.\n\n![SerilogAnalyzer](https://raw.githubusercontent.com/nblumhardt/images/master/serilog-analyzer-example.gif)\n\n## Installing (Visual Studio)\n\nYou can get the SerilogAnalyzer from various sources:\n- Grab the VSIX from the [releases list](https://github.com/Suchiman/SerilogAnalyzer/releases).\n- Install it from [Visual Studio Gallery](https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer).\n- Install the [NuGet package](https://www.nuget.org/packages/SerilogAnalyzer) into your project.\n\n## Analyses\n\n### `Serilog001`: Exception Usage\n\nChecks that exceptions are passed to the `exception` argument, and not as a normal property, with a code fix to correct it.\n\n**Detected incorrect usage:**\n\n```csharp\ncatch (Exception ex)\n{\n   Log.Error(\"Could not save {File}: {Error}\", file, ex);\n}\n```\n\nThe `ex` parameter is an exception, which Serilog has special handling for if passed as the first argument.\n\n**Correct usage:**\n\n```csharp\ncatch (Exception ex)\n{\n   Log.Error(ex, \"Could not save {File}\", file);\n}\n```\n\n### `Serilog002`: Message Template Syntax Verifier\n\nChecks message templates for correct syntax and emits an error if there's a violation of the templating syntax.\n\n**Detected incorrect usage:**\n\n```csharp\nLog.Information(\"Saving {File to {Directory}\", file, directory);\n```\n\nThe first property token in the message template, `File`, is malformed.\n\n**Correct usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", file, directory);\n```\n\n### `Serilog003`: Property Binding Verifier\n\nChecks coherence between the message template tokens and the supplied arguments.\n\n**Detected incorrect usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", file);\n```\n\nHere the number of arguments passed to the method (1) is less than the number of tokens in the message template (2), so the second token in the message template, `{Directory}`, will have no value.\n\n**Correct usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", file, directory);\n```\n\nEach property named in the message template needs to correspond to exactly one argument.\n\n### `Serilog004`: Constant Message Template Verifier\n\nChecks that message templates are constant strings. This ensures that events with different data/format arguments can still be detected as instances of the same event.\n\n**Detected incorrect usage:**\n\n```csharp\nvar errorMessage = TryToCheckOutOrder(...); // etc.\nLog.Error(errorMessage);\n```\n\nBecause `errorMessage` generally contains failure-specific text (`\"Couldn't find order 123\"` ... then `124`, then `125`) the group of occurrences can't be located using the message template/event type.\n\nThis also degrades Serilog performance by filling its internal message template cache.\n\n**Correct usage:**\n\n```csharp\nLog.Error(\"Order handler failed with {HandlerError}\", errorMessage);\n```\n\nCorrect usage is to always pass any variable data as a property to a message template.\nA CodeFix is provided that converts string interpolation (`$\"{...}\"`), `String.Format(...)` and string concat (`\"value: \" + value`) to a message template\n\n### `Serilog005`: Unique Property Name Verifier\n\nChecks that all property names in a message template are unique.\n\n**Detected incorrect usage:**\n\n```csharp\nLog.Information(\"Saving {Path} to {Path}\", file, directory); \n```\n\nIn this example, because both properties in the message template have the same name, Serilog can only record one of them.\n\n**Correct usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", file, directory); \n```\n\nEach property in a message template must have a unique name.\n\n### `Serilog006`: Pascal Cased Property Verifier\n\nChecks that all property names in a message template are PascalCased.\n\n**Detected incorrect usage:**\n\n```csharp\nLog.Information(\"Saving {file} to {directory}\", file, directory); \n```\n\nA CodeFix is provided, that applies pascal casing.\n\n**Correct usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", file, directory); \n```\n\n### `Serilog007`: Anonymous objects use destructuring Verifier\n\nChecks that all anonymous objects passed to the logger are destructured.\n\n**Detected incorrect usage:**\n\n```csharp\nLog.Information(\"Saving {File} to {Directory}\", new { Name = name, Size = size }, directory); \n```\n\nA CodeFix is provided, that applies the destructuring hint.\n\n**Correct usage:**\n\n```csharp\nLog.Information(\"Saving {@File} to {Directory}\", new { Name = name, Size = size }, directory); \n```\n\n### `Serilog008`: Correct contextual Logger Verifier\n\nChecks that contextual loggers are constructed with the correct type.\n\n**Detected incorrect usage:**\n\n```csharp\nclass A\n{\n    private static readonly ILogger Logger = Logger.ForContext\u003cB\u003e();\n}\n\nclass B {}\n```\n\nA CodeFix is provided, that uses the correct type.\n\n**Correct usage:**\n\n```csharp\nclass A\n{\n    private static readonly ILogger Logger = Logger.ForContext\u003cA\u003e();\n}\n\nclass B {} \n```\n\n## Refactors\nPerforms static analysis on a fluent LoggerConfiguration call to generate configuration for use with either [`\u003cappSettings\u003e`](https://github.com/serilog/serilog-settings-appsettings) or [`appSettings.json`](https://github.com/serilog/serilog-settings-configuration)\n\n![Serilog Configuration Refactoring](./Github/serilog-configuration-refactoring.gif)\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具"],"sub_categories":["Tools","工具"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSuchiman%2FSerilogAnalyzer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSuchiman%2FSerilogAnalyzer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSuchiman%2FSerilogAnalyzer/lists"}