icecream 2.0.3
package reference too old
See the version list below for details.
dotnet add package icecream --version 2.0.3
NuGet\Install-Package icecream -Version 2.0.3
<PackageReference Include="icecream" Version="2.0.3" />
paket add icecream --version 2.0.3
#r "nuget: icecream, 2.0.3"
// Install icecream as a Cake Addin #addin nuget:?package=icecream&version=2.0.3 // Install icecream as a Cake Tool #tool nuget:?package=icecream&version=2.0.3
icecream-csharp
IceCream — Never use print() to debug again
Do you ever use Console.WriteLine()
to debug your code? Of course you
do. IceCream, or ic
for short, makes print debugging a little sweeter.
ic()
is like print()
in Python, except it's better:
- Detailed Printing: IceCream prints not only values but also contextual information, including the filename, timestamp, line number, label, and parent function (optional).
- Redesigned for C#: The tool has been redesigned to work with C#.
- Simplicity: IceCream is designed for simplicity and is 60% faster to use compared to other debugging tools.
- Rich Output Formatting: IceCream offers the capability to format and colorize your debugging output in one line, enhancing its informativeness and visual appeal.
- Flexible Configuration: You can configure various settings in IceCream to customize your debugging output according to your specific needs.
- Output Customization: You can further customize the debugging output by adding labels, prefixes, and more to suit your preferences.
IceCream is well tested, permissively licensed, and supports mostly all versions of .NET.
Install and Import
First, install IceCream with NuGet.
$ dotnet add package icecream
Then, import it in your code.
using icecream;
Quick Start
"Hello World".ic("Label");
var x = 12.ic() + 2;
x.ic("x");
(1, 2).ic();
Prints as:
🍧| Program.cs:1 in Main() at 06:33:13.723 - Label: "Hello World"
🍧| Program.cs:2 in Main() at 06:33:13.761 - 12
🍧| Program.cs:3 in Main() at 06:33:13.766 - x: 14
🍧| Program.cs:4 in Main() at 06:33:13.767 - {Item1: 1, Item2: 2}
As you can see, you can add .ic()
almost ANYWHERE and have no impact on the code logic because it returns the original
value. They don't have syntax color in this README document, but they do in your console. 😃
In Python, ic(foo(123))
could print something like ic| foo(123): 456
. However, it's impossible to do that in
Java or C# because of the way the language is designed. In this case I believe obj.ic()
is more elegant
than ic(obj)
.
You can easily add them and remove them all with Replace All
in your IDE.
However, if you really want to use the traditional way, you can still do it with ic(obj)
. You need
to using static icecream.IceCreamTraditional;
first.
Usually, you don't need to add a label to the output because the context information is already enough.
While, you can still add an optional label to the output by passing a string as .ic(label)
like the first example.
Print Anything
We use JsonConvert.SerializeObject()
(all versions supported) to convert the object to a string in default. It is
powerful and is able to print
much more types than JsonSerializer.Serialize()
. You can define your own ArgToStringFunction
to parse the object to
a string in your own
way too.
_str = "abc";
_num = 123;
_dbl = 123.456;
_boolean = true;
_obj = new { a = 1, b = "2", c = new { d = 3, e = new { f = 4 } } };
_dict = new Dictionary<string, object>
{
{ "a", 1 },
{ "b", "2" },
{ "c", new { d = 3, e = new { f = 4 } } },
{ "d", new Dictionary<string, TestClass> { { "test", new TestClass() } } }
};
_list = new List<object> { 1, "2", new { d = 3, e = new { f = 4 } }, new TestClass() };
_arr = new string[] { "a", "b", "c" };
_kvp = new KeyValuePair<string, object>("a", 1);
_tuple = (1, 3.14f, true, new TestClass());
_testClass = new TestClass();
_testEnum = TestEnum.A;
🍧| Program.cs:15 in Main() at 06:52:17.146 - "abc"
🍧| Program.cs:15 in Main() at 06:52:17.189 - 123
🍧| Program.cs:15 in Main() at 06:52:17.192 - 123.456
🍧| Program.cs:15 in Main() at 06:52:17.193 - true
🍧| Program.cs:15 in Main() at 06:52:17.194 - {a: 1, b: "2", c: {d: 3, e: {f: 4}}}
🍧| Program.cs:15 in Main() at 06:52:17.224 - {a: 1, b: "2", c: {d: 3, e: {f: 4}}, d: {test: {PublicInt: 2, PublicString: "public"}}}
🍧| Program.cs:15 in Main() at 06:52:17.230 - [1, "2", {d: 3, e: {f: 4}}, {PublicInt: 2, PublicString: "public"}]
🍧| Program.cs:15 in Main() at 06:52:17.232 - ["a", "b", "c"]
🍧| Program.cs:15 in Main() at 06:52:17.233 - {Key: "a", Value: 1}
🍧| Program.cs:15 in Main() at 06:52:17.236 - {Item1: 1, Item2: 3.14, Item3: true, Item4: {PublicInt: 2, PublicString: "public"}}
🍧| Program.cs:15 in Main() at 06:52:17.238 - {PublicInt: 2, PublicString: "public"}
🍧| Program.cs:15 in Main() at 06:52:17.238 - A
Logging
.IceFormat()
is like ic()
but the output is returned as a string instead
var str = "hello".IceFormat();
str
is now 🍧| Program.cs:1 in Main() at 06:33:13.723 - "hello"
.
Enable/Disable
IceCream.Enable(); // Enable IceCream
IceCream.Disable(); // Disable IceCream
Configuration
Here's a overview of the settings:
public class IceCreamSettings
{
public bool IncludeContext { get; set; } = true;
public string Prefix { get; set; } = "\ud83c\udf67| ";
public bool UseAbsPath { get; set; } = false;
public Action<string> OutputAction { get; set; } = null;
public Func<object, string> ArgToStringFunction { get; set; } = null;
public ConsoleColor? LabelColor { get; set; } = ConsoleColor.DarkBlue;
public ConsoleColor? FieldColor { get; set; } = ConsoleColor.DarkRed;
public ConsoleColor? ValueColor { get; set; } = ConsoleColor.DarkCyan;
public Encoding Encoding { get; set; } = Encoding.UTF8;
}
Use IceCream.Configure(IceCreamSettings settings)
to configure IceCream. You can only set the properties you want to
change, and the rest will be set to default values. Also, call IceCream.Configure()
directly resets all settings.
IncludeContext
(default:true
): Whether to include context information (line number, parent function, etc.) in the output.Prefix
(default:🍧|
): The prefix of the output.UseAbsPath
(default:false
): Whether to use absolute path of the file or the file name only.OutputAction
(default:null
): The action to output the result. If it isnull
, the result will be output toConsole.WriteLine()
with the color set inLabelColor
,FieldColor
andValueColor
.ArgToStringFunction
(default:obj => JsonConvert.SerializeObject(obj, new StringEnumConverter())
): The function converting the object to a string. If it isnull
, the default function will be used.LabelColor
(default:ConsoleColor.DarkBlue
): The color of the label.FieldColor
(default:ConsoleColor.DarkRed
): The color of the field.ValueColor
(default:ConsoleColor.DarkCyan
): The color of the value.Encoding
(default:Encoding.UTF8
): The encoding of the output.
Alternatively, you can use IceCream.SetXxx(newValue)
(e.g. IceCream.SetPrefix("ic>")
) to set a single setting. These
functions won't reset other settings.
IceCream in Other Languages
Delicious IceCream should be enjoyed in every language.
- Python: icecream
- Dart: icecream
- Rust: icecream-rs
- Node.js: node-icecream
- C++: IceCream-Cpp
- C99: icecream-c
- PHP: icecream-php
- Go: icecream-go
- Ruby: Ricecream
- Java: icecream-java
- R: icecream
- Lua: icecream-lua
- Clojure(Script): icecream-cljc
- Bash: IceCream-Bash
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Newtonsoft.Json (>= 3.5.8)
-
.NETFramework 4.5
- Newtonsoft.Json (>= 3.5.8)
-
.NETStandard 2.0
- Newtonsoft.Json (>= 3.5.8)
-
net5.0
- Newtonsoft.Json (>= 3.5.8)
-
net6.0
- Newtonsoft.Json (>= 3.5.8)
-
net7.0
- Newtonsoft.Json (>= 3.5.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.2.0 | 723 | 11/13/2023 |
Doc fix