Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AppendKeyValueAscii methods #41

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions src/ZeroLog.Tests/LogEventTests.KeyValues.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using NUnit.Framework;

namespace ZeroLog.Tests
Expand Down Expand Up @@ -27,14 +28,80 @@ public void should_append_multiple_key_values()
[Test]
public void should_append_formatted_string_mixed_with_key_values()
{
// TODO(lmanners): There are more edge cases here.
_logEvent.AppendKeyValue("myKey", "myValue");
_logEvent.AppendFormat("Some {} message");
_logEvent.Append("formatted");
_logEvent.AppendKeyValue("otherKey", 2);
_logEvent.Append("...");
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual("Some formatted message ~~ { \"myKey\": \"myValue\", \"otherKey\": 2 }", _output.ToString());
Assert.AreEqual("Some formatted message... ~~ { \"myKey\": \"myValue\", \"otherKey\": 2 }", _output.ToString());
}

[Test]
public void should_append_key_byte_array_value()
{
var bytes = Encoding.ASCII.GetBytes("myValue");
_logEvent.AppendKeyValueAscii("myKey", bytes, bytes.Length);
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual(" ~~ { \"myKey\": \"myValue\" }", _output.ToString());
}

[Test]
public void should_append_key_byte_span_value()
{
var bytes = Encoding.ASCII.GetBytes("myValue");
_logEvent.AppendKeyValueAscii("myKey", bytes.AsSpan());
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual(" ~~ { \"myKey\": \"myValue\" }", _output.ToString());
}

[Test]
public void should_append_key_char_span_value()
{
_logEvent.AppendKeyValueAscii("myKey", "myValue".AsSpan());
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual(" ~~ { \"myKey\": \"myValue\" }", _output.ToString());
}

[Test]
public void should_ignore_byte_array_value_with_negative_length()
{
var bytes = Encoding.Default.GetBytes("myValue");
_logEvent.AppendKeyValueAscii("myKey", bytes, -1);
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual("", _output.ToString());
}

[Test]
public void should_ignore_byte_array_value_that_is_empty()
lmanners marked this conversation as resolved.
Show resolved Hide resolved
{
_logEvent.AppendKeyValueAscii("myKey", new byte[0], 0);
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual("", _output.ToString());
}

[Test]
public void should_ignore_byte_span_value_that_is_empty()
{
_logEvent.AppendKeyValueAscii("myKey", ReadOnlySpan<byte>.Empty);
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual("", _output.ToString());
}

[Test]
public void should_ignore_char_span_value_that_is_empty()
{
_logEvent.AppendKeyValueAscii("myKey", ReadOnlySpan<char>.Empty);
_logEvent.WriteToStringBuffer(_output);

Assert.AreEqual("", _output.ToString());
}

[Test]
Expand Down
54 changes: 54 additions & 0 deletions src/ZeroLog/LogEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,60 @@ public ILogEvent AppendKeyValue<T>(string key, T? value)
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ILogEvent AppendKeyValueAscii(string key, byte[]? bytes, int length)
{
if (length <= 0 || !PrepareAppend(sizeof(ArgumentType) + sizeof(byte) + sizeof(ArgumentType) + sizeof(int) + length, 2))
return this;

AppendArgumentType(ArgumentType.KeyString);
AppendString(key);

AppendAsciiString(bytes, length);
ltrzesniewski marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ILogEvent AppendKeyValueAscii(string key, byte* bytes, int length)
{
if (length <= 0 || !PrepareAppend(sizeof(ArgumentType) + sizeof(byte) + sizeof(ArgumentType) + sizeof(int) + length, 2))
return this;

AppendArgumentType(ArgumentType.KeyString);
AppendString(key);

AppendAsciiString(bytes, length);
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ILogEvent AppendKeyValueAscii(string key, ReadOnlySpan<byte> bytes)
{
var length = bytes.Length;
if (length <= 0 || !PrepareAppend(sizeof(ArgumentType) + sizeof(byte) + sizeof(ArgumentType) + sizeof(int) + length, 2))
return this;

AppendArgumentType(ArgumentType.KeyString);
AppendString(key);

AppendAsciiString(bytes);
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ILogEvent AppendKeyValueAscii(string key, ReadOnlySpan<char> bytes)
{
var length = bytes.Length;
if (length <= 0 || !PrepareAppend(sizeof(ArgumentType) + sizeof(byte) + sizeof(ArgumentType) + sizeof(int) + length, 2))
return this;

AppendArgumentType(ArgumentType.KeyString);
AppendString(key);

AppendAsciiString(bytes);
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ILogEvent AppendAsciiString(byte[]? bytes, int length)
{
Expand Down