Skip to content

Commit

Permalink
Merge pull request PHOENIXCONTACT#70 from PHOENIXCONTACT/clean/logging
Browse files Browse the repository at this point in the history
Clean up of logging in kernel
  • Loading branch information
Toxantron authored Nov 3, 2020
2 parents e61e5a3 + f08bc6d commit 130eb91
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 345 deletions.
77 changes: 77 additions & 0 deletions src/Moryx.Runtime.Kernel/Logging/CommonLoggingLogTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2020, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using Common.Logging;
using Common.Logging.Simple;
using Moryx.Logging;
using LogLevel = Moryx.Logging.LogLevel;

namespace Moryx.Runtime.Kernel
{
internal class CommonLoggingLogTarget : ILogTarget
{
private readonly ILog _internalTarget;

public CommonLoggingLogTarget(string name)
{
try
{
_internalTarget = LogManager.GetLogger(name);
}
catch
{
_internalTarget = new NoOpLogger();
}
}

public void Log(LogLevel logLevel, string message)
{
Log(logLevel, message, null);
}

public void Log(LogLevel logLevel, string message, Exception exception)
{
var isException = exception != null;
switch (logLevel)
{
case LogLevel.Trace:
if (isException)
_internalTarget.Trace(message, exception);
else
_internalTarget.Trace(message);
break;
case LogLevel.Debug:
if (isException)
_internalTarget.Debug(message, exception);
else
_internalTarget.Debug(message);
break;
case LogLevel.Info:
if (isException)
_internalTarget.Info(message, exception);
else
_internalTarget.Info(message);
break;
case LogLevel.Warning:
if (isException)
_internalTarget.Warn(message, exception);
else
_internalTarget.Warn(message);
break;
case LogLevel.Error:
if (isException)
_internalTarget.Error(message, exception);
else
_internalTarget.Error(message);
break;
case LogLevel.Fatal:
if (isException)
_internalTarget.Fatal(message, exception);
else
_internalTarget.Fatal(message);
break;
}
}
}
}
Loading

0 comments on commit 130eb91

Please sign in to comment.