forked from PHOENIXCONTACT/MORYX-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PHOENIXCONTACT#70 from PHOENIXCONTACT/clean/logging
Clean up of logging in kernel
- Loading branch information
Showing
6 changed files
with
85 additions
and
345 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Moryx.Runtime.Kernel/Logging/CommonLoggingLogTarget.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.