Skip to content

Commit

Permalink
Feature/logger (#1)
Browse files Browse the repository at this point in the history
* Added Logger service
  • Loading branch information
NickKvizhinadze committed Nov 12, 2020
1 parent 8cf5955 commit 2f13fa1
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 15 deletions.
6 changes: 3 additions & 3 deletions DotNetHelpers.MvcCore/ApiBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public abstract class ApiBaseController : ControllerBase
protected ActionResult<T> CustomResult<T>(Result<T> result)
{
if (!result.Succeeded)
return ReturnError(result);
return Error(result);

return Ok(result.Data);
}

protected IActionResult CustomResult(Result result)
{
if (!result.Succeeded)
return ReturnError(result);
return Error(result);

return NoContent();
}

//TODO: rename to ErrorResult
protected ActionResult ReturnError(Result result)
protected ActionResult Error(Result result)
{
AddErrors(result.Errors);
switch (result.ErrorStatus)
Expand Down
8 changes: 1 addition & 7 deletions DotNetHelpers.MvcCore/Config.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetHelpers.MvcCore
namespace DotNetHelpers.MvcCore
{
public static class Config
{
Expand Down
9 changes: 5 additions & 4 deletions DotNetHelpers.Sample/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using DotNetHelpers.Sample.Enums;
using DotNetHelpers.Sample.Models;
using DotNetHelpers.Sample.Factories;
using DotNetHelpers.Logger;

namespace DotNetHelpers.Sample.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ILogService _logger;

public HomeController(ILogger<HomeController> logger)
public HomeController(ILogService logger)
{
_logger = logger;
}
Expand All @@ -39,9 +40,9 @@ public IActionResult Index(int page = 1, int perPage = 10)
}
catch (Exception ex)
{
_logger.LogError($"Error type: {ErrorType.Internal.GetDisplayName()}, Error message: {ex.Message}");
throw;
_logger.LogError(null, $"{nameof(HomeController)} => {nameof(Index)}", $"Error type: {ErrorType.Internal.GetDisplayName()}", ex);
}
return null;
}

public IActionResult Privacy()
Expand Down
6 changes: 6 additions & 0 deletions DotNetHelpers.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetHelpers.Logger;
using DotNetHelpers.MvcCore.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -24,6 +25,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILogService, LogService>();
services.AddControllersWithViews();
}

Expand All @@ -32,6 +34,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
DotNetHelpers.MvcCore.Config.Init(25);

new DotNetHelpers.Logger.LoggerConfiguration()
.SetConnectionString("Server=KVIZHINADZE;Database=MeetingRooms;Trusted_Connection=True;")
.Build();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down
2 changes: 1 addition & 1 deletion DotNetHelpers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetHelpers.MvcCore", "Do
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetHelpers.Sample", "DotNetHelpers.Sample\DotNetHelpers.Sample.csproj", "{77040DD9-77CD-4218-9C27-FB133CE52F1C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetHelpers.Tests", "DotNetHelpers.Tests\DotNetHelpers.Tests.csproj", "{9AF5DEB2-92EC-4761-BFEF-291300B6F581}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetHelpers.Tests", "DotNetHelpers.Tests\DotNetHelpers.Tests.csproj", "{9AF5DEB2-92EC-4761-BFEF-291300B6F581}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 2 additions & 0 deletions DotNetHelpers/DotNetHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="Vsxmd" Version="1.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
42 changes: 42 additions & 0 deletions DotNetHelpers/Enums/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace DotNetHelpers.Enums
{
// <summary>:
// Defines logging severity levels.
/// <summary>
public enum LogLevel
{
// <summary>:
// Logs that contain the most detailed messages. These messages may contain sensitive
// application data. These messages are disabled by default and should never be
// enabled in a production environment.
/// <summary>
Trace = 0,
// <summary>:
// Logs that are used for interactive investigation during development. These logs
// should primarily contain information useful for debugging and have no long-term
// value.
/// <summary>
Debug = 1,
// <summary>:
// Logs that track the general flow of the application. These logs should have long-term
// value.
/// <summary>
Information = 2,
// <summary>:
// Logs that highlight an abnormal or unexpected event in the application flow,
// but do not otherwise cause the application execution to stop.
/// <summary>
Warning = 3,
// <summary>:
// Logs that highlight when the current flow of execution is stopped due to a failure.
// These should indicate a failure in the current activity, not an application-wide
// failure.
/// <summary>
Error = 4,
// <summary>:
// Logs that describe an unrecoverable application or system crash, or a catastrophic
// failure that requires immediate attention.
/// <summary>
Critical = 5
}
}
33 changes: 33 additions & 0 deletions DotNetHelpers/Exceptions/LoggerException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace DotNetHelpers.Exceptions
{
/// <summary>
/// Logger Exception
/// </summary>
[Serializable]
public class LoggerException : Exception
{
/// <summary>
/// Empty Constructor
/// </summary>
public LoggerException()
{
}

/// <summary>
/// Constructor with messages
/// </summary>
public LoggerException(string message) : base(message)
{
}

/// <summary>
/// Constructor with messages and inner exception
/// </summary>
public LoggerException(string message, Exception innerException) : base(message, innerException)
{
}

}
}
46 changes: 46 additions & 0 deletions DotNetHelpers/Logger/ILogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Threading.Tasks;

namespace DotNetHelpers.Logger
{
public interface ILogService
{
#region Error
Task LogError(string eventId, string eventName, string message);
Task LogError(string eventId, string eventName, Exception exception);
Task LogError(string eventId, string eventName, string message, Exception exception);
#endregion

#region Warning
Task LogWarning(string eventId, string eventName, string message);
Task LogWarning(string eventId, string eventName, Exception exception);
Task LogWarning(string eventId, string eventName, string message, Exception exception);
#endregion

#region Information
Task LogInformation(string eventId, string eventName, string message);
Task LogInformation(string eventId, string eventName, Exception exception);
Task LogInformation(string eventId, string eventName, string message, Exception exception);
#endregion


#region Trace
Task LogTrace(string eventId, string eventName, string message);
Task LogTrace(string eventId, string eventName, Exception exception);
Task LogTrace(string eventId, string eventName, string message, Exception exception);
#endregion

#region Debug
Task LogDebug(string eventId, string eventName, string message);
Task LogDebug(string eventId, string eventName, Exception exception);
Task LogDebug(string eventId, string eventName, string message, Exception exception);
#endregion


#region Critical
Task LogCritical(string eventId, string eventName, string message);
Task LogCritical(string eventId, string eventName, Exception exception);
Task LogCritical(string eventId, string eventName, string message, Exception exception);
#endregion
}
}

0 comments on commit 2f13fa1

Please sign in to comment.