Skip to content

Commit

Permalink
NLog GoogleStackdriverTarget improve handling of System.Reflection ob…
Browse files Browse the repository at this point in the history
…jects that will dump all types in the the application-assembly.
  • Loading branch information
snakefoot authored and jskeet committed Apr 20, 2020
1 parent f476856 commit ced67e4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ public async Task SingleLogEntryWithJsonProperties()
Assert.Equal("Ocean", properties.Fields["PlanetType"].StringValue);
}

[Fact]
public async Task SingleLogEntryWithBadJsonProperty()
{
var uploadedEntries = await RunTestWorkingServer(
googleTarget =>
{
LogManager.GetLogger("testlogger").Info("Hello {BadBoy}", new BadObject());
return Task.FromResult(0);
}, includeEventProperties: true, configFn: googleTarget => googleTarget.SendJsonPayload = true);
Assert.Single(uploadedEntries);
var entry0 = uploadedEntries[0];
Assert.Equal("", entry0.TextPayload?.Trim() ?? "");
Assert.Equal("Hello BadObject", entry0.JsonPayload.Fields["message"].StringValue);

var properties = entry0.JsonPayload.Fields["properties"].StructValue;

Assert.Equal(1, properties.Fields.Count);
Assert.StartsWith("Google.Cloud.Logging.NLog.Tests", properties.Fields["BadBoy"].StructValue.Fields["Assembly"].StringValue);
}

[Fact]
public async Task SingleLogEntryWithJsonCollectionProperties()
{
Expand Down Expand Up @@ -590,5 +610,22 @@ public async Task GaePlatform_NoConfiguredProjectId()
// the monitored resource is at.
Assert.Equal("gae_project_id", uploadedEntries[0].LogNameAsLogName.ProjectId);
}

private class BadObject
{
public System.Collections.Generic.List<object> BadList { get; } = new System.Collections.Generic.List<object>();

public System.Reflection.Assembly Assembly => typeof(BadObject).Assembly;

public BadObject()
{
BadList.Add(this);
}

public override string ToString()
{
return nameof(BadObject);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ protected override void InitializeTarget()
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
jsonSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
jsonSettings.Converters.Add(new ToStringJsonConverter(typeof(MethodInfo)));
jsonSettings.Converters.Add(new ToStringJsonConverter(typeof(Assembly)));
jsonSettings.Converters.Add(new ToStringJsonConverter(typeof(Module)));
jsonSettings.Error = (sender, args) =>
{
// Serialization of properties that throws exceptions should not break everything
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Newtonsoft.Json;

namespace Google.Cloud.Logging.NLog
{
internal sealed class ToStringJsonConverter : JsonConverter
{
private readonly System.Type _type;

/// <inheritdoc />
public override bool CanRead { get; } = false;

public ToStringJsonConverter(System.Type type)
{
_type = type;
}

/// <inheritdoc />
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(value.ToString());
}
}

/// <inheritdoc />
public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotSupportedException("Only serialization is supported");
}

/// <inheritdoc />
public override bool CanConvert(System.Type objectType)
{
return _type.IsAssignableFrom(objectType);
}
}
}

0 comments on commit ced67e4

Please sign in to comment.