{"id":19626577,"url":"https://github.com/softcircuits/simplelogfile","last_synced_at":"2025-10-18T08:50:23.225Z","repository":{"id":98675227,"uuid":"264552340","full_name":"SoftCircuits/SimpleLogFile","owner":"SoftCircuits","description":".NET class that makes it easy to write log entries to a file.","archived":false,"fork":false,"pushed_at":"2024-08-06T03:18:31.000Z","size":76,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-09T00:49:43.493Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SoftCircuits.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.txt","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":"2020-05-17T00:15:21.000Z","updated_at":"2024-08-06T03:18:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"7fc93e00-64fa-4733-aef1-8a6e14e2f3ba","html_url":"https://github.com/SoftCircuits/SimpleLogFile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FSimpleLogFile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FSimpleLogFile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FSimpleLogFile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FSimpleLogFile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftCircuits","download_url":"https://codeload.github.com/SoftCircuits/SimpleLogFile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224098941,"owners_count":17255545,"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":[],"created_at":"2024-11-11T11:47:02.527Z","updated_at":"2025-10-18T08:50:23.128Z","avatar_url":"https://github.com/SoftCircuits.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimpleLogFile\n\n[![NuGet version (SoftCircuits.SimpleLogFile)](https://img.shields.io/nuget/v/SoftCircuits.SimpleLogFile.svg?style=flat-square)](https://www.nuget.org/packages/SoftCircuits.SimpleLogFile/)\n\n```\nInstall-Package SoftCircuits.SimpleLogFile\n```\n\n## Overview\n\nYet another log-file class for .NET. This one was designed to be dead simple to use when writing log entries to a file. (Although it can be extended to write entries elsewhere.)\n\nThe class supports different log entry levels, which can be filtered or disabled entirely. The library has special handling for formatting exceptions, and can optionally write all inner exceptions. Virtual functions can be overridden to control log entry formatting, where and how log entries are written, and what the `Delete()` method does.\n\nThe library does not keep the log file open. The file is opened each time a log entry needs to be written and then immediately closed. This ensures that all log entries are flushed to disk even if the program crashes unexpectedly. The class was also designed not to raise any exceptions if the log filename is set to `null`. In this case, logging is simply disabled.\n\n## Using the Library\n\nTo use the library, start by creating an instance of the `LogFile` class. Then call any of the log methods to write a log entry.\n\n```cs\nLogFile logFile = new LogFile(path);\n\n// Log entries with different importance, or 'levels'\nlogFile.LogInfo(\"An information-level log entry\");\nlogFile.LogWarning(\"A warning-level log entry\");\nlogFile.LogError(\"An error-level log entry\");\nlogFile.LogCritical(\"A critical-level log entry\");\n\n// A divider helps separate groups of log entries\nlogFile.LogDivider();\n\n// An entry can include any number of objects\nlogFile.LogError(\"An error-level log entry\", 12345, 'n', \"Error\");\n\n// The library has special handling for formatting exceptions. LogFile properties control\n// whether inner exceptions are written, and whether the name of the exception type\n// includes the namespace.\nlogFile.LogError(\"This parameter is required\", new ArgumentNullException(\"parameterName\"));\n\n// And you can log formatted entries\nlogFile.LogErrorFormat(\"Expected {0} items, but found {1} items\", 25, 26);\n```\n\nThe code above produces the following log entries.\n\n```\n[5/16/2020 5:56:54 PM][INFO] An information-level log entry\n[5/16/2020 5:56:54 PM][WARNING] A warning-level log entry\n[5/16/2020 5:56:54 PM][ERROR] An error-level log entry\n[5/16/2020 5:56:54 PM][CRITICAL] A critical-level log entry\n-------------------------------------------------------------------------------\n[5/16/2020 5:56:54 PM][ERROR] An error-level log entry : 12345 : n : Error\n[5/16/2020 5:56:54 PM][ERROR] This parameter is required : ArgumentNullException: Value cannot be null. (Parameter 'parameterName')\n[5/16/2020 5:56:54 PM][ERROR] Expected 25 items, but found 26 items\n```\n\n## Log Levels\n\nEach of the log methods shown in the previous example specifies the log entry importance, or *level*. The `LogFile` class also has a `LogLevel` property. This allows you to easily filter out lower level log entries.\n\nIf the log entry's level is not equal to or higher than the `LogFile`'s `LogLevel` property, the entry will not be written to the log file. If the `LogLevel` property of the `LogFile` class is set to `LogLevel.None`, no log entries will be written and logging is effectively disabled.\n\n```cs\nlogFile.LogLevel = LogLevel.Warning;\n\n// This entry will not be written because LogLevel.Info is a lower level than LogLevel.Warning\nlogFile.LogInfo(\"This is an information-level entry\");\n\n// This entry will be written because LogLevel.Error is a higher level than LogLevel.Warning\nlogFile.LogError(\"This is an error-level entry\");\n\nlogFile.LogLevel = LogLevel.None;\n\n// Now even a critical log entry will not be written\nlogFile.LogCritical(\"This is an error-level entry\");\n```\n\nIn addition to the methods mentioned above, you can also use the `Log()` method. This method takes a `LogLevel` argument, and requires a little more typing.\n\n```cs\n// These two lines are equivalent\nlogFile.LogError(\"An error-level log entry\");\nlogFile.Log(LogLevel.Error, \"An error-level log entry\");\n```\n\n## Exceptions\n\nSimpleLogFile has some special handling for .NET exceptions (`Exception` and classes that derive from it). Consider the code below.\n\n```cs\n// Create an exception with inner exceptions\nException ex = new ArgumentNullException(\"parameterName\");\nex = new InvalidOperationException(\"Unable to do that\", ex);\nex = new InvalidDataException(\"Unable to do this\", ex);\nex = new InvalidProgramException(\"There was a problem!\", ex);\n\n// No inner exceptions logged here\nlogFile.LogError(\"Something went wrong\", ex);\n\n// Now log inner exceptions\nlogFile.LogInnerExceptions = true;\nlogFile.LogError(\"Something went wrong\", ex);\n```\n\nThe code above would produce the following log entries.\n\n```\n[5/16/2020 6:38:50 PM][ERROR] Something went wrong : InvalidProgramException: There was a problem!\n[5/16/2020 6:38:50 PM][ERROR] Something went wrong : InvalidProgramException: There was a problem!\n : [INNER EXCEPTION] InvalidDataException: Unable to do this\n : [INNER EXCEPTION] InvalidOperationException: Unable to do that\n : [INNER EXCEPTION] ArgumentNullException: Value cannot be null. (Parameter 'parameterName')\n```\n\n## Deleting the Log File\n\nIn cases where you want to start a fresh log file, the `Delete()` method is provided. It simply deletes the current log file.\n\n```cs\nlogFile.Delete();\n```\n\n## Overriding Behavior\n\nThis class was designed to be as straight forward and simple to use as possible. But it can be extended somewhat by deriving your class from `LogFile` and overriding one of the virtual methods.\n\nEach of these methods are documented below.\n\n```cs\nprotected virtual string OnFormat(LogLevel level, string text);\n```\n\nThis method formats the text before it gets written to the log file. Override this method to change the formatting.\n\n```cs\nprotected virtual string OnFormatSecondary(LogLevel level, string text);\n```\n\nThis method formats the text for a secondary log entry before it gets written to the log file. Currently, the only secondary log entries supported are for the inner exceptions when `LogInnerExceptions` is true. Override this method to change the formatting of secondary entries.\n\n```cs\nprotected virtual string OnFormatException(Exception ex);\n```\n\nThis method formats an exception that is part of a log entry. Note that when logging inner exceptions, this method will be called for each inner exception. And so it should normally format only the top-level exception.\n\n```cs\nprotected virtual void OnWrite(string text);\n```\n\nThis method performs the task of writing a formatted entry to the log file. You can override this method if you want to change the way the entry is written, or write it to another location.\n\n```cs\nprotected virtual void OnDelete();\n```\n\nThis method deletes the log file. Most likely, you will only need to override this method if you have also overridden `OnWrite()` and need to delete a different file or take other action.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcircuits%2Fsimplelogfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftcircuits%2Fsimplelogfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcircuits%2Fsimplelogfile/lists"}