{"id":22519673,"url":"https://github.com/cocowalla/serilog-sinks-file-header","last_synced_at":"2025-08-03T18:32:45.940Z","repository":{"id":56412356,"uuid":"183036744","full_name":"cocowalla/serilog-sinks-file-header","owner":"cocowalla","description":"Plugin for the Serilog File sink that writes a header at the start of each log file","archived":false,"fork":false,"pushed_at":"2020-11-09T15:36:36.000Z","size":29,"stargazers_count":10,"open_issues_count":2,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-06T20:15:35.110Z","etag":null,"topics":["headers","logging","serilog"],"latest_commit_sha":null,"homepage":null,"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/cocowalla.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}},"created_at":"2019-04-23T14:49:16.000Z","updated_at":"2023-12-14T10:20:43.000Z","dependencies_parsed_at":"2022-08-15T18:10:52.059Z","dependency_job_id":null,"html_url":"https://github.com/cocowalla/serilog-sinks-file-header","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocowalla%2Fserilog-sinks-file-header","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocowalla%2Fserilog-sinks-file-header/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocowalla%2Fserilog-sinks-file-header/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocowalla%2Fserilog-sinks-file-header/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cocowalla","download_url":"https://codeload.github.com/cocowalla/serilog-sinks-file-header/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228560789,"owners_count":17937135,"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":["headers","logging","serilog"],"created_at":"2024-12-07T04:25:43.381Z","updated_at":"2024-12-07T04:25:44.077Z","avatar_url":"https://github.com/cocowalla.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serilog.Sinks.File.Header\n[![NuGet](https://img.shields.io/nuget/v/Serilog.Sinks.File.Header.svg)](https://www.nuget.org/packages/Serilog.Sinks.File.Header)\n[![Build status](https://ci.appveyor.com/api/projects/status/initq19hswan7q4u?svg=true)](https://ci.appveyor.com/project/cocowalla/serilog-sinks-file-header)\n\nA `FileLifecycleHooks`-based plugin for the [Serilog File Sink](https://github.com/serilog/serilog-sinks-file) that writes a configurable header at the start of each log file.\n\n### Getting started\nTo get started, install the latest [Serilog.Sinks.File.Header](https://www.nuget.org/packages/Serilog.Sinks.File.Header) package from NuGet:\n\n```powershell\nInstall-Package Serilog.Sinks.File.Header -Version 1.0.1\n```\n\nTo enable writing a header, use one of the new `LoggerSinkConfiguration` extensions that has a `FileLifecycleHooks` argument, and create a new `HeaderWriter`:\n\n```csharp\nLog.Logger = new LoggerConfiguration()\n    .WriteTo.File(\"log.txt\", hooks: new HeaderWriter(\"Timestamp,Level,Message\"))\n    .CreateLogger();\n```\n\nNote this also works if you enable rolling log files.\n\nInstead of writing a static string, you can instead provide a factory method that resolves the header at runtime:\n\n```csharp\nFunc\u003cstring\u003e headerFactory = () =\u003e $\"My dynamic header {DateTime.UtcNow}\";\n\nLog.Logger = new LoggerConfiguration()\n    .WriteTo.File(\"log.txt\", hooks: new HeaderWriter(headerFactory))\n    .CreateLogger();\n```\n\n### JSON appsettings.json configuration\nIt's also possible to enable log file headers  when configuring Serilog from a configuration file using [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration/). To do this, you will first need to create a class with a public static member that can provide the configuration system with a configured instance of `HeaderWriter`:\n\n```csharp\nusing Serilog.Sinks.File.Header;\n\nnamespace MyApp.Logging\n{\n    public class SerilogHooks\n    {\n        public static HeaderWriter MyHeaderWriter =\u003e new HeaderWriter(\"Timestamp,Level,Message\");\n    }\n}\n```\n\nThe `hooks` argument in Your `appsettings.json` file should be configured as follows:\n\n```json\n{\n  \"Serilog\": {\n    \"WriteTo\": [\n      {\n        \"Name\": \"File\",\n        \"Args\": {\n          \"path\": \"log.csv\",\n          \"hooks\": \"MyApp.Logging.SerilogHooks::MyHeaderWriter, MyApp\"\n        }\n      }\n    ]\n  }\n}\n```\n\nTo break this down a bit, what you are doing is specifying the fully qualified type name of the class that provides your `HeaderWriter`, using `Serilog.Settings.Configuration`'s special `::` syntax to point to the `MyHeaderWriter` member.\n\n### Sample applications\n2 basic console apps are provided as samples:\n\n- [Serilog.Sinks.File.Header.Sample](https://github.com/cocowalla/serilog-sinks-file-header/tree/master/sample/Serilog.Sinks.File.Header.Sample): demonstrates writing tab-delimited logs with a header row, configured programmatically\n- [Serilog.Sinks.File.Header.ConfigSample](https://github.com/cocowalla/serilog-sinks-file-header/tree/master/sample/Serilog.Sinks.File.Header.ConfigSample): demonstrates writing CSV-formatted logs with a header row, configured from appsettings.json\n\nIn both projects, logs are written to files in `.\\Logs`.\n\n### About `FileLifecycleHooks`\n`FileLifecycleHooks` is a Serilog File Sink mechanism that allows hooking into log file lifecycle events, enabling scenarios such as wrapping the Serilog output stream in another stream, or capturing files before they are deleted by Serilog's retention mechanism.\n\nOther available hooks include:\n\n- [serilog-sinks-file-gzip](https://github.com/cocowalla/serilog-sinks-file-gzip): compresses logs as they are written, using streaming GZIP compression\n- [serilog-sinks-file-archive](https://github.com/cocowalla/serilog-sinks-file-archive): archives completed log files before they are deleted by Serilog's retention mechanism\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocowalla%2Fserilog-sinks-file-header","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcocowalla%2Fserilog-sinks-file-header","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocowalla%2Fserilog-sinks-file-header/lists"}