![]() |
VOOZH | about |
dotnet add package Chickensoft.Log --version 2.0.0
NuGet\Install-Package Chickensoft.Log -Version 2.0.0
<PackageReference Include="Chickensoft.Log" Version="2.0.0" />
<PackageVersion Include="Chickensoft.Log" Version="2.0.0" />Directory.Packages.props
<PackageReference Include="Chickensoft.Log" />Project file
paket add Chickensoft.Log --version 2.0.0
#r "nuget: Chickensoft.Log, 2.0.0"
#:package Chickensoft.Log@2.0.0
#addin nuget:?package=Chickensoft.Log&version=2.0.0Install as a Cake Addin
#tool nuget:?package=Chickensoft.Log&version=2.0.0Install as a Cake Tool
Opinionated logging interface and implementations for C# applications and libraries.
<p align="center"> <img alt="Chickensoft.Log" src="Chickensoft.Log/icon.png" width="200"> </p>
For logging in Godot, see Chickensoft.Log.Godot.
Install the latest version of the Chickensoft.Log package from nuget:
dotnet add package Chickensoft.Log
In Chickensoft.Log, messages are logged through the ILog interface. Each ILog
has a name (often the name of the class using the ILog), an ILogFormatter for
formatting messages, and a collection of ILogWriters, each responsible for
directing log messages to one output.
The package includes a standard implementation of ILog, named Log. To create
a log, instantiate a Log and give it a name and at least one writer:
public class MyClass
{
// Create a log with the name of MyClass, outputting to stdout/stderr
private ILog _log = new Log(nameof(MyClass), new ConsoleWriter());
}
You can give the log multiple writers to obtain multiple outputs:
public class MyClass
{
// Create a log with the name of MyClass, outputting to stdout/stderr and
// .NET's Trace system
private ILog _log = new Log(nameof(MyClass), new ConsoleWriter(),
new TraceWriter());
}
To log messages, use ILog's methods Print(), Warn(), and Err().
Example:
public class MyClass
{
public void MyMethod()
{
// Outputs "Info (MyClass): A log message"
_log.Print("A log message");
// Outputs "Warn (MyClass): A warning message"
_log.Warn("A warning message");
// Outputs "Error (MyClass): An error occurred"
_log.Err("An error occurred");
try
{
SomethingThatThrows();
}
catch (Exception e)
{
// Outputs the value of e.ToString(), prefixed by a line labeling it an exception,
// as an error
_log.Print(e);
}
// Outputs the current stack trace as a standard log message
_log.Print(new System.Diagnostics.StackTrace());
}
}
Some writers may have separate channels for warnings and errors, while others
may not. For instance, the TraceWriter has separate channels for regular log
messages, warnings, and errors. The FileWriter has only one channel, the
file it's writing to. Warnings and errors can still be distinguished by the
label the formatter gives them, even if directed to the same channel as regular
log messages.
Optionally, when constructing a log, you can provide an ILogFormatter that the
log will use to format each message. (The formatted message should include
the log's name, the level of the message, and the message itself.)
public class MyClass
{
private ILog _log = new Log(nameof(MyClass), new ConsoleWriter())
{
Formatter = new MyFormatter()
};
}
By default, Log will use the included LogFormatter class implementing
ILogFormatter.
Messages are formatted with one of three level labels, depending which log method
you call. By default, the included LogFormatter uses the labels "Info",
"Warn", and "Error". You can change these labels for an individual
LogFormatter:
var formatter = new LogFormatter()
{
MessagePrefix = "INFO";
WarningPrefix = "WARN";
ErrorPrefix = "ERROR";
};
You can also change the default values of these labels for all LogFormatters:
LogFormatter.DefaultMessagePrefix = "INFO";
LogFormatter.DefaultWarningPrefix = "WARN";
LogFormatter.DefaultErrorPrefix = "ERROR";
Changing the default values of the level labels will affect newly-created
LogFormatters, but will not affect ones that already exist.
Log accepts a parameter array of writers, which receive formatted messages
from the log and are responsible for handling the output of the messages. The
Log package provides three writer types for use in applications or libraries:
ConsoleWriter: Outputs log messages to stdout and stderr.TraceWriter: Outputs log messages to .NET's Trace system. This is useful
for seeing log output in Visual Studio's "Output" tab while debugging.FileWriter: Outputs log messages to file. By default, FileWriter will
write to a file called "output.log" in the working directory, but you can either
configure a different default, or configure individual FileWriters to write to
particular files on creation. To avoid concurrency issues, FileWriter is
implemented as a pseudo-singleton with a single instance per file name; see
below for details.The package provides one additional writer type, TestWriter, which may be
useful for testing your code without mocking ILog (see below).
TraceWriterTo see TraceWriter's output from a Godot application in Visual Studio's
Output pane, add a DefaultTraceListener to the system's list of Trace
listeners somewhere near your entry point:
using System.Diagnostics;
public class MyGodotApp : Node
{
public override void _Ready()
{
Trace.Listeners.Add(new DefaultTraceListener());
}
}
This step is necessary with GoDotTest test suites as well as games (or any other Godot-based applications).
This step is unnecessary if you are running or debugging in Visual Studio
Code, so you may want to guard adding DefaultTraceListener with a
command-line flag.
FileWriterFileWriter provides two static Instance() methods for obtaining references
to writers.
You can obtain a reference to a writer using the default file name "output.log":
public class MyClass
{
private ILog _log = new Log(nameof(MyClass), FileWriter.Instance());
}
You can obtain a writer that outputs messages to a custom file name:
public class MyClass
{
private ILog _log = new Log(nameof(MyClass),
FileWriter.Instance("CustomFileName.log"));
}
And you can change the default file name for FileWriters:
public class Entry
{
public static void Main()
{
// Change the default file name for FileWriter before any writers are created
FileWriter.DefaultFileName = "MyFileName.log";
// ...
}
}
public class MyClass
{
// Create a FileWriter that writes to the new default name
private ILog _log = new Log(nameof(MyClass), FileWriter.Instance());
}
Changing the default value for the log file name will affect newly-created
FileWriters, but will not affect ones that already exist.
TestWriterWhen testing code that uses an ILog, it may be cumbersome to mock ILog's
methods. In that case, you may prefer to use the provided TestWriter type,
which accumulates log messages for testing:
// Class under test
public class MyClass
{
public ILog Log { get; set; } = new Log(nameof(MyClass), new ConsoleWriter());
// Method that logs some information; we want to test the logged messages
public void MyMethod()
{
Log.Print("A normal log message");
Log.Err("An error message");
}
}
public class MyClassTest
{
[Fact]
public void MyMethodLogs()
{
// set up an instance of MyClass, but with a TestWriter instead of a ConsoleWriter
var testWriter = new TestWriter();
var obj = new MyClass() { Log = new Log(nameof(MyClass), testWriter) };
obj.MyMethod();
// use TestWriter to test the logging behavior of MyClass
testWriter.LoggedMessages
.ShouldBeEquivalentTo(new List<string>
{
"Info (MyClass): A normal log message",
"Error (MyClass): An error message"
});
}
}
TestWriter is not thread-safe.
Having issues? We'll be happy to help you in the Chickensoft Discord server.
🐣 Package generated from a 🐤 Chickensoft Template — https://chickensoft.games
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on Chickensoft.Log:
| Package | Downloads |
|---|---|
|
Chickensoft.GoDotTest
C# test runner for Godot. Run tests from the command line, collect code coverage, and debug tests in VSCode. |
|
|
Chickensoft.Log.Godot
Opinionated logging for C# in Godot, based on Chickensoft.Log. |
Showing the top 1 popular GitHub repositories that depend on Chickensoft.Log:
| Repository | Stars |
|---|---|
|
chickensoft-games/GoDotTest
C# test runner for Godot. Run tests from the command line, collect code coverage, and debug tests.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0 | 24,654 | 10/11/2025 |
| 1.1.4 | 723 | 10/2/2025 |
| 1.1.3 | 486 | 9/28/2025 |
| 1.1.2 | 2,546 | 9/10/2025 |
| 1.1.1 | 805 | 9/3/2025 |
| 1.1.0 | 909 | 8/28/2025 |
| 1.0.17 | 4,469 | 8/16/2025 |
| 1.0.16 | 1,120 | 8/11/2025 |
| 1.0.15 | 1,130 | 8/6/2025 |
| 1.0.14 | 1,986 | 7/17/2025 |
| 1.0.13 | 475 | 7/14/2025 |
| 1.0.12 | 660 | 7/9/2025 |
| 1.0.11 | 1,870 | 6/26/2025 |
| 1.0.10 | 1,215 | 6/11/2025 |
| 1.0.9 | 221 | 6/7/2025 |
| 1.0.8 | 755 | 6/3/2025 |
| 1.0.7 | 680 | 5/23/2025 |
| 1.0.6 | 509 | 5/20/2025 |
| 1.0.5 | 513 | 5/14/2025 |
| 1.0.4 | 730 | 5/8/2025 |
Chickensoft.Log release.