Created
September 20, 2023 18:37
-
-
Save keyan1603/af105a028a25e1f58c3ca9db4e0f5c43 to your computer and use it in GitHub Desktop.
Sitecore Helix – Feature based logging framework
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
using System; | |
namespace MySite.Foundation.Common | |
{ | |
public interface ILoggerService | |
{ | |
#region Debug | |
void Debug(string message); | |
void Debug(string message, Exception ex); | |
#endregion | |
#region Info | |
void Info(string message); | |
void Info(string message, Exception ex); | |
#endregion | |
#region Warn | |
void Warn(string message); | |
void Warn(string message, Exception ex); | |
#endregion | |
#region Error | |
void Error(string message); | |
void Error(string message, Exception ex); | |
#endregion | |
#region Fatal | |
void Fatal(string message); | |
void Fatal(string message, Exception ex); | |
#endregion | |
} | |
} | |
using log4net; | |
using Sitecore.Diagnostics; | |
using System; | |
using System.Text; | |
using System.Web; | |
namespace MySite.Foundation.Common | |
{ | |
public abstract class LoggerService : ILoggerService | |
{ | |
private readonly ILog _log; | |
private const string AspNetSessionKey = "ASP.NET_SessionId"; | |
protected LoggerService(string logger) | |
{ | |
_log = LoggerFactory.GetLogger(logger); | |
} | |
void ILoggerService.Debug(string message) | |
{ | |
_log.Debug(PrependTraceId(message)); | |
} | |
void ILoggerService.Debug(string message, Exception ex) | |
{ | |
_log.Debug(message, ex); | |
} | |
void ILoggerService.Info(string message) | |
{ | |
_log.Info(PrependTraceId(message)); | |
} | |
void ILoggerService.Info(string message, Exception ex) | |
{ | |
_log.Info(message, ex); | |
} | |
void ILoggerService.Warn(string message) | |
{ | |
_log.Warn(PrependTraceId(message)); | |
} | |
void ILoggerService.Warn(string message, Exception ex) | |
{ | |
_log.Warn(PrependTraceId(message), ex); | |
} | |
void ILoggerService.Error(string message) | |
{ | |
_log.Error(PrependTraceId(message)); | |
} | |
void ILoggerService.Error(string message, Exception ex) | |
{ | |
_log.Error(PrependTraceId(message), ex); | |
} | |
void ILoggerService.Fatal(string message) | |
{ | |
_log.Fatal(PrependTraceId(message)); | |
} | |
void ILoggerService.Fatal(string message, Exception ex) | |
{ | |
_log.Fatal(message, ex); | |
} | |
private string PrependTraceId(string message) | |
{ | |
var builder = new StringBuilder(); | |
if (HttpContext.Current?.Request?.Cookies?[AspNetSessionKey] != null) | |
{ | |
var aspSessionId = HttpContext.Current.Request.Cookies[AspNetSessionKey].Value; | |
if (!string.IsNullOrWhiteSpace(aspSessionId)) | |
{ | |
builder.Append("AspNetSessionID:"); | |
builder.Append(aspSessionId); | |
builder.Append(" "); | |
} | |
} | |
builder.Append(message); | |
return builder.ToString(); | |
} | |
} | |
} | |
using MySite.Foundation.Common; | |
namespace MySite.Feature.Insights.Logging | |
{ | |
internal class Logger : LoggerService | |
{ | |
public Logger() : base("MySite.Feature.Products") | |
{ } | |
} | |
} | |
private ILoggerService logger = new Logger(); | |
// To log as Debug | |
logger.Debug("test"); | |
// To log an error during exception ex | |
logger.Error("test", ex); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment