Skip to content

Commit

Permalink
Merge a18b044 into 6b8adca
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Apr 10, 2020
2 parents 6b8adca + a18b044 commit e1168c8
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 4 deletions.
3 changes: 2 additions & 1 deletion exampleapps/apps/test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class GlobalApp : NetDaemonApp
public override async Task InitializeAsync()
{
SharedThing = "Hello world";

Log("Logging from global app");
LogError("OMG SOMETING IS WRONG {error}", "The error!");
}
}

Expand Down
3 changes: 3 additions & 0 deletions exampleapps/apps/test2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class BatteryManager : NetDaemonApp
public string? HelloWorldSecret { get; set; }
public override async Task InitializeAsync()
{
Log("Hello");
Log("Hello {name}", "Tomas");

// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
// var app = (GlobalApp)GetApp("global_app");
Expand Down
2 changes: 1 addition & 1 deletion exampleapps/apps/test2.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app:
my_app:
class: BatteryManager
# HelloWorldSecret: !secret test_secret
dependencies:
Expand Down
83 changes: 81 additions & 2 deletions src/App/NetDaemon.App/Common/NetDaemonApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,89 @@ public Task SaveDataAsync<T>(string id, T data)
}

/// <inheritdoc/>
public void Log(string message, LogLevel level = LogLevel.Information) => Logger.Log(level, message);
public void Log(string message) => Log(LogLevel.Information, message);

/// <inheritdoc/>
public void Log(string message, Exception exception, LogLevel level = LogLevel.Information) => Logger.Log(level, exception, message);
public void LogWarning(string message) => Log(LogLevel.Warning, message);

/// <inheritdoc/>
public void LogError(string message) => Log(LogLevel.Error, message);

/// <inheritdoc/>
public void LogTrace(string message) => Log(LogLevel.Trace, message);

/// <inheritdoc/>
public void LogDebug(string message) => Log(LogLevel.Debug, message);

/// <inheritdoc/>
public void Log(Exception exception, string message) => Log(LogLevel.Information, exception, message);
/// <inheritdoc/>
public void LogWarning(Exception exception, string message) => Log(LogLevel.Warning, exception, message);
/// <inheritdoc/>
public void LogError(Exception exception, string message) => Log(LogLevel.Error, exception, message);
/// <inheritdoc/>
public void LogDebug(Exception exception, string message) => Log(LogLevel.Debug, exception, message);
/// <inheritdoc/>
public void LogTrace(Exception exception, string message) => Log(LogLevel.Trace, exception, message);

/// <inheritdoc/>
public void Log(LogLevel level, string message, params object[] param)
{
if (param is object && param.Length > 0)
{
var result = param.Prepend(Id).ToArray();
Logger.Log(level, $" {{Id}}: {message}", result);
}
else
{
Logger.Log(level, $" {{Id}}: {message}", new object[] { Id ?? "" });
}
}

/// <inheritdoc/>
public void Log(string message, params object[] param) => Log(LogLevel.Information, message, param);

/// <inheritdoc/>
public void LogWarning(string message, params object[] param) => Log(LogLevel.Warning, message, param);

/// <inheritdoc/>
public void LogError(string message, params object[] param) => Log(LogLevel.Error, message, param);

/// <inheritdoc/>
public void LogDebug(string message, params object[] param) => Log(LogLevel.Debug, message, param);

/// <inheritdoc/>
public void LogTrace(string message, params object[] param) => Log(LogLevel.Trace, message, param);


/// <inheritdoc/>
public void Log(LogLevel level, Exception exception, string message, params object[] param)
{
if (param is object && param.Length > 0)
{
var result = param.Prepend(Id).ToArray();
Logger.Log(level, exception, $" {{Id}}: {message}", result);
}
else
{
Logger.Log(level, exception, $" {{Id}}: {message}", new object[] { Id ?? "" });
}
}

/// <inheritdoc/>
public void Log(Exception exception, string message, params object[] param) => Log(LogLevel.Information, exception, message, param);

/// <inheritdoc/>
public void LogWarning(Exception exception, string message, params object[] param) => Log(LogLevel.Warning, exception, message, param);

/// <inheritdoc/>
public void LogError(Exception exception, string message, params object[] param) => Log(LogLevel.Error, exception, message, param);

/// <inheritdoc/>
public void LogDebug(Exception exception, string message, params object[] param) => Log(LogLevel.Debug, exception, message, param);

/// <inheritdoc/>
public void LogTrace(Exception exception, string message, params object[] param) => Log(LogLevel.Trace, exception, message, param);

/// <inheritdoc/>
public IMediaPlayer MediaPlayer(params string[] entityIds)
Expand Down
32 changes: 32 additions & 0 deletions tests/NetDaemon.Daemon.Tests/LoggerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,37 @@ public void AssertLogged(LogLevel level, Times times)
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), times);
}

/// <summary>
/// Assert if the log has been used at times
/// </summary>
/// <param name="level">The loglevel being checked</param>
/// <param name="times">The Times it has been logged</param>
public void AssertLogged(LogLevel level, string message, Times times)
{
MockLogger.Verify(
x => x.Log(
level,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString() == message),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), times);
}

/// <summary>
/// Assert if the log has been used at times
/// </summary>
/// <param name="level">The loglevel being checked</param>
/// <param name="times">The Times it has been logged</param>
public void AssertLogged(LogLevel level, Exception exception, string message, Times times)
{
MockLogger.Verify(
x => x.Log(
level,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString() == message),
exception,
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), times);
}
}
}
87 changes: 87 additions & 0 deletions tests/NetDaemon.Daemon.Tests/NetDaemonApp/NetDaemonAppTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,108 @@
using JoySoftware.HomeAssistant.NetDaemon.Common;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Reflection;
using Xunit;

namespace NetDaemon.Daemon.Tests.NetDaemonApp
{
public class NetDaemonApptests
{
private readonly LoggerMock _logMock;
private Mock<INetDaemon> _netDaemonMock;

private const string appTemplate = " app: ";
private JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp _app;

public NetDaemonApptests()
{
_logMock = new LoggerMock();
_netDaemonMock = new Mock<INetDaemon>();
_netDaemonMock.SetupGet(n => n.Logger).Returns(_logMock.Logger);

_app = new JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp();
_app.StartUpAsync(_netDaemonMock.Object);
_app.Id = "app";
}


[Theory]
[InlineData(LogLevel.Information, "Log")]
[InlineData(LogLevel.Warning, "LogWarning")]
[InlineData(LogLevel.Error, "LogError")]
[InlineData(LogLevel.Trace, "LogTrace")]
[InlineData(LogLevel.Debug, "LogDebug")]
public void LogMessageWithDifferentLogLevelsShoulCallCorrectLogger(LogLevel level, string methodName)
{
// ARRANGE
var message = "message";
MethodInfo? methodInfo;

// ACT
methodInfo = _app.GetType().GetMethod(methodName, new Type[] { typeof(string) });

methodInfo?.Invoke(_app, new object[] { message });
// ASSERT
_logMock.AssertLogged(level, appTemplate + message, Times.Once());
}

[Theory]
[InlineData(LogLevel.Information, "Log")]
[InlineData(LogLevel.Warning, "LogWarning")]
[InlineData(LogLevel.Error, "LogError")]
[InlineData(LogLevel.Trace, "LogTrace")]
[InlineData(LogLevel.Debug, "LogDebug")]
public void LogMessageWithParamsAndDifferentLogLevelsShoulCallCorrectLogger(LogLevel level, string methodName)
{
// ARRANGE
var message = "Hello {name}";

// ACT
var methodInfo = _app.GetType().GetMethod(methodName, new Type[] { typeof(string), typeof(object[]) });

methodInfo?.Invoke(_app, new object[] { message, new object[] { "Bob" } });
// ASSERT
_logMock.AssertLogged(level, appTemplate + "Hello Bob", Times.Once());
}

[Theory]
[InlineData(LogLevel.Information, "Log")]
[InlineData(LogLevel.Warning, "LogWarning")]
[InlineData(LogLevel.Error, "LogError")]
[InlineData(LogLevel.Trace, "LogTrace")]
[InlineData(LogLevel.Debug, "LogDebug")]
public void LogMessageWithParamsExceptionAndDifferentLogLevelsShoulCallCorrectLogger(LogLevel level, string methodName)
{
// ARRANGE
var message = "Hello {name}";
var exception = new NullReferenceException("Null");
// ACT
var methodInfo = _app.GetType().GetMethod(methodName, new Type[] { typeof(Exception), typeof(string), typeof(object[]) });

methodInfo?.Invoke(_app, new object[] { exception, message, new object[] { "Bob" } });
// ASSERT
_logMock.AssertLogged(level, exception, appTemplate + "Hello Bob", Times.Once());
}

[Theory]
[InlineData(LogLevel.Information, "Log")]
[InlineData(LogLevel.Warning, "LogWarning")]
[InlineData(LogLevel.Error, "LogError")]
[InlineData(LogLevel.Trace, "LogTrace")]
[InlineData(LogLevel.Debug, "LogDebug")]
public void LogMessageWithExceptionAndDifferentLogLevelsShoulCallCorrectLogger(LogLevel level, string methodName)
{
// ARRANGE
var message = "message";
var exception = new NullReferenceException("Null");
// ACT
var methodInfo = _app.GetType().GetMethod(methodName, new Type[] { typeof(Exception), typeof(string) });

methodInfo?.Invoke(_app, new object[] { exception, message });
// ASSERT
_logMock.AssertLogged(level, exception, appTemplate + message, Times.Once());
}

[Fact]
Expand Down

0 comments on commit e1168c8

Please sign in to comment.