diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/src/ServiceStack.Serilog.RequestLogsFeature.sln b/src/ServiceStack.Serilog.RequestLogsFeature.sln new file mode 100644 index 0000000..8780991 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27004.2005 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.Serilog.RequestLogsFeature", "ServiceStack.Serilog.RequestLogsFeature\ServiceStack.Serilog.RequestLogsFeature.csproj", "{1ABEF0BF-672E-4559-AFC8-B28138CCB19B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1ABEF0BF-672E-4559-AFC8-B28138CCB19B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1ABEF0BF-672E-4559-AFC8-B28138CCB19B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1ABEF0BF-672E-4559-AFC8-B28138CCB19B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1ABEF0BF-672E-4559-AFC8-B28138CCB19B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8A43D06A-F29A-4F9A-9ECA-2D13FAAA8190} + EndGlobalSection +EndGlobal diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEntryPropertiesGenerator.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEntryPropertiesGenerator.cs new file mode 100644 index 0000000..fbab0fb --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEntryPropertiesGenerator.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; +using Serilog.Events; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Logging +{ + public delegate IEnumerable LogEntryPropertiesGenerator(IRequest request, object requestDto, object responseDto); +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEventFactory.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEventFactory.cs new file mode 100644 index 0000000..6695d71 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/LogEventFactory.cs @@ -0,0 +1,166 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Serilog; +using Serilog.Events; +using Serilog.Parsing; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Logging +{ + internal class LogEventFactory + { + private static readonly string Property_HttpMethod_Key = "Method"; + private static readonly string Property_Url_Key = "Url"; + private static readonly string Property_StatusCode_Key = "StatusCode"; + private static readonly string Property_StatusDesc_Key = "StatusDescription"; + private static readonly string Property_Elapsed_Key = "Elasped"; + private static readonly string Property_Headers_Key = "Headers"; + private static readonly string Property_Body_Key = "Body"; + private static readonly string Property_Form_Key = "Form"; + private static readonly string Property_ReqDto_Key = "RequestDto"; + private static readonly string Property_Session_Key = "Session"; + private static readonly string Property_Response_Key = "Response"; + private static readonly string Property_Error_Key = "Error"; + + private static readonly MessageTemplate LogEventMessageTemplate = + new MessageTemplateParser().Parse($@"HTTP {{{Property_HttpMethod_Key}}} {{{Property_Url_Key}}} responded {{{Property_StatusCode_Key}}} in {{{Property_Elapsed_Key}}} ms"); + + internal LogEvent Create(IRequest request, object requestDto, object responseDto, TimeSpan elapsed, RequestLoggerOptions opt) + => new LogEvent( + timestamp: DateTimeOffset.Now, + exception: null, + level: LogEventLevel.Information, + messageTemplate: LogEventFactory.LogEventMessageTemplate, + properties: GetProperties(request, requestDto, responseDto, elapsed, opt) + ); + + private static IEnumerable GetProperties(IRequest request, object requestDto, object responseDto, TimeSpan elapsed, RequestLoggerOptions opt) + { + yield return WithHttpMethod(request); + yield return WithUrl(request); + yield return WithHeaders(request); + yield return WithStatusCode(request); + yield return WithStatusDescription(request); + yield return WithElapsedTime(elapsed); + + + bool IsRequestDtoExcludedFromLogging(Type dtoType) => opt.HideRequestBodyForRequestDtoTypes != null && opt.HideRequestBodyForRequestDtoTypes.Any(@type => @type != dtoType); + Type requestDtoType = (requestDto ?? request.Dto)?.GetType(); + if (requestDtoType != null + && !IsRequestDtoExcludedFromLogging(requestDtoType) + && opt.EnableRequestBodyTracking + ) + { + yield return WithRequestBody(request); + yield return WithFormData(request); + yield return WithRequestDto(request, requestDto); + } + + + if (opt.EnableSessionTracking) yield return WithSession(request); + if (opt.EnableResponseTracking) yield return WithResponse(request, responseDto); + + if (request.IsErrorResponse() && opt.EnableErrorTracking) + { + var prop = WithErrorResponse(request, responseDto); + if (prop != null) yield return prop; + } + + foreach (var prop in WithPropertiesFromDelegate(request, requestDto, responseDto, opt.LogEntryPropertiesGenerator)) + yield return prop; + + + yield break; + } + + private static LogEventProperty WithHttpMethod(IRequest request) + => new LogEventProperty(Property_HttpMethod_Key, new ScalarValue(request.Verb.ToUpper())); + + private static LogEventProperty WithUrl(IRequest request) + => new LogEventProperty(Property_Url_Key, new ScalarValue(request.PathInfo)); + + private static LogEventProperty WithStatusCode(IRequest request) + => new LogEventProperty(Property_StatusCode_Key, new ScalarValue(request.Response.StatusCode)); + + private static LogEventProperty WithStatusDescription(IRequest request) + => new LogEventProperty(Property_StatusDesc_Key, new ScalarValue(request.Response.StatusDescription)); + + private static LogEventProperty WithHeaders(IRequest request) + { + var headersAsLogEventProps = request + .Headers + .ToDictionary() + .Select(dictItem => new LogEventProperty(dictItem.Key, new ScalarValue(dictItem.Value))) + ; + + return new LogEventProperty(Property_Headers_Key, new StructureValue(headersAsLogEventProps)); + } + + private static LogEventProperty WithRequestBody(IRequest request) + => new LogEventProperty(Property_Body_Key, new ScalarValue(request?.GetRawBody() ?? String.Empty)); + + private static LogEventProperty WithFormData(IRequest request){ + var formDataAsLogEventProps = request + .FormData + .ToDictionary() + .Select(dictItem => new LogEventProperty(dictItem.Key, new ScalarValue(dictItem.Value))) + ; + + return new LogEventProperty(Property_Form_Key, new StructureValue(formDataAsLogEventProps)); + } + + private static LogEventProperty WithRequestDto(IRequest request, object requestDto){ + var dto = requestDto ?? request.Dto; + var logger = request.Items[Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey] as ILogger; + logger.BindProperty(Property_ReqDto_Key, dto, true, out LogEventProperty logEventProperty); + + return logEventProperty; + } + + private static LogEventProperty WithSession(IRequest request) + { + var session = request.GetSession(); + var logger = request.Items[Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey] as ILogger; + logger.BindProperty(Property_Session_Key, session, true, out LogEventProperty logEventProperty); + + return logEventProperty; + } + + private static LogEventProperty WithResponse(IRequest request, object responseDto) + { + var logger = request.Items[Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey] as ILogger; + logger.BindProperty(Property_Response_Key, responseDto, true, out LogEventProperty logEventProperty); + + return logEventProperty; + } + + private static LogEventProperty WithElapsedTime(TimeSpan elapsed) + => new LogEventProperty(Property_Elapsed_Key, new ScalarValue(Math.Ceiling(elapsed.TotalMilliseconds))); + + private static LogEventProperty WithErrorResponse(IRequest request, object responseDto) + { + var logger = request.Items[Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey] as ILogger; + + + if (responseDto is IHttpResult errorResult) + { + logger.BindProperty(Property_Error_Key, errorResult.Response, true, out LogEventProperty logEventProperty); + return logEventProperty; + } + + if(responseDto is Exception exception) + { + var responseStatus = (exception.InnerException ?? exception).ToResponseStatus(); + logger.BindProperty(Property_Error_Key, responseStatus, true, out LogEventProperty logEventProperty); + return logEventProperty; + } + + return null; + } + + private static IEnumerable WithPropertiesFromDelegate(IRequest request, object requestDto, object responseDto, LogEntryPropertiesGenerator propertiesGenerator) + => propertiesGenerator != null ? propertiesGenerator.Invoke(request, requestDto, responseDto) : Enumerable.Empty(); + + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLogger.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLogger.cs new file mode 100644 index 0000000..a0f73c3 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLogger.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Serilog; +using ServiceStack.Host; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Logging +{ + public class RequestLogger : IRequestLogger + { + private LogEventFactory LogEventFactory{ get; } = new LogEventFactory(); + private InMemoryRollingRequestLogger LatestLogEntriesCollector { get; } = new InMemoryRollingRequestLogger(capacity:1000); + private RequestLoggerOptions Options { get; set; } = new RequestLoggerOptions(); + + public bool EnableSessionTracking { get => Options.EnableSessionTracking; + set => LatestLogEntriesCollector.EnableSessionTracking = Options.EnableSessionTracking = value; } + public bool EnableRequestBodyTracking { get => Options.EnableRequestBodyTracking; + set => LatestLogEntriesCollector.EnableRequestBodyTracking = Options.EnableRequestBodyTracking = value; } + public bool EnableResponseTracking { get => Options.EnableResponseTracking; + set => LatestLogEntriesCollector.EnableResponseTracking = Options.EnableResponseTracking = value; } + public bool EnableErrorTracking { get => Options.EnableErrorTracking; + set => LatestLogEntriesCollector.EnableErrorTracking = Options.EnableErrorTracking = value; } + public bool LimitToServiceRequests { get => Options.LimitToServiceRequests; + set => LatestLogEntriesCollector.LimitToServiceRequests = Options.LimitToServiceRequests = value; } + public string[] RequiredRoles { get => Options.RequiredRoles; + set => LatestLogEntriesCollector.RequiredRoles = Options.RequiredRoles = value; } + public Func SkipLogging { get => Options.SkipLogging; + set => LatestLogEntriesCollector.SkipLogging = Options.SkipLogging = value; } + public Type[] ExcludeRequestDtoTypes { get => Options.ExcludeRequestDtoTypes; + set => LatestLogEntriesCollector.ExcludeRequestDtoTypes = Options.ExcludeRequestDtoTypes = value; } + public Type[] HideRequestBodyForRequestDtoTypes { get => Options.HideRequestBodyForRequestDtoTypes; + set => Options.HideRequestBodyForRequestDtoTypes = value; } + + + public LogEntryPropertiesGenerator LogEntryPropertiesGenerator { get => Options.LogEntryPropertiesGenerator; + set => Options.LogEntryPropertiesGenerator = value; } + + + public List GetLatestLogs(int? take) + { + return LatestLogEntriesCollector.GetLatestLogs(take); + } + + public void Log(IRequest request, object requestDto, object response, TimeSpan elapsed) + { + if(!AssertCanLog(request, requestDto)) + return; + + RequestLoggerOptions loggingOptions = Options.Clone() as RequestLoggerOptions; + + if (request.Items.ContainsKey(Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey)) + { + + var logEvent = LogEventFactory + .Create(request, requestDto, response, elapsed, loggingOptions); + + if(logEvent != null) + { + var logger = request.Items[Plugin.SerilogRequestLogsFeature.SerilogRequestLogsLoggerKey] as ILogger; + + logger + .ForContext() + .Write(logEvent); + } + + LatestLogEntriesCollector + .Log(request, requestDto, response, elapsed); + } + } + + private bool AssertCanLog(IRequest request, object requestDto) => ShouldNotLog(request, requestDto).All(r => r == false); + + private IEnumerable ShouldNotLog(IRequest request, object requestDto) + { + yield return request == null; + + if (SkipLogging != null) + yield return SkipLogging.Invoke(request); + + if (LimitToServiceRequests) + yield return (requestDto ?? request?.Dto) == null; + + if (RequiredRoles != null && RequiredRoles.Any()) + yield return RequiredRoles.Except(request?.GetSession()?.Roles).Any() == false; + + if (LimitToServiceRequests && ExcludeRequestDtoTypes != null && ExcludeRequestDtoTypes.Any()) + yield return (requestDto ?? request.Dto) == null || ExcludeRequestDtoTypes.Contains((requestDto ?? request.Dto).GetType()); + + yield break; + + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLoggerOptions.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLoggerOptions.cs new file mode 100644 index 0000000..204083c --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Logging/RequestLoggerOptions.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using Serilog; +using Serilog.Events; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Logging +{ + + internal class RequestLoggerOptions : ICloneable + { + public bool EnableSessionTracking { get; set; } + public bool EnableRequestBodyTracking { get; set; } + public bool EnableResponseTracking { get; set; } + public bool EnableErrorTracking { get; set; } + public bool LimitToServiceRequests { get; set; } + public string[] RequiredRoles { get; set; } + public Func SkipLogging { get; set; } + public Type[] ExcludeRequestDtoTypes { get; set; } + public Type[] HideRequestBodyForRequestDtoTypes { get; set; } + public LogEntryPropertiesGenerator LogEntryPropertiesGenerator { get; set; } + + internal RequestLoggerOptions GetCopy() + { + return (RequestLoggerOptions)MemberwiseClone(); + } + + public object Clone() + { + return GetCopy(); + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/Feature.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/Feature.cs new file mode 100644 index 0000000..e242de2 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/Feature.cs @@ -0,0 +1,124 @@ +using System; +using System.Threading.Tasks; +using Serilog; +using Serilog.Core; +using ServiceStack.Serilog.RequestLogsFeature.Logging; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Plugin +{ + public class SerilogRequestLogsFeature : IPlugin + { + private readonly FeatureValidator Validator = new FeatureValidator(); + + public const string SerilogRequestLogsLoggerKey = "SerilogRequestLogs.Logger"; + + /// + /// SerilogRequestLogs service Route, default is /serilogrequestlogs + /// + public string AtRestPath { get; set; } + + /// + /// Request Logger instance + /// + public IRequestLogger RequestLogger { get; set; } + + /// + /// Delegate used to construct custom serilog logger instance + /// + public Func SerilogLoggerFactory { get; set; } + + /// + /// Delegate used to configure local instance of Serilog logger. + /// It should not be used with delegate. + /// + public Func SerilogLoggerBuilder { get; set; } + + /// + /// Delegate used to provide collection of properties included in log entry. + /// + public LogEntryPropertiesGenerator LogEntryPropertiesGenerator{ get; set; } + + /// + /// Collection of roles that user should have for logging. + /// + public string[] RequiredRoles { get; set; } + + /// + /// Delegate used to check if log entry should be created. + /// + public Func SkipLogging { get; set; } + + /// + /// Collection of request dtos' types that shouldn't be logged. + /// + public Type[] ExcludeRequestDtoTypes { get; set; } + + /// + /// Collection of request dtos' types that should not be included in log entry. + /// + public Type[] HideRequestBodyForRequestDtoTypes { get; set; } + + public SerilogRequestLogsFeature() + { + AtRestPath = "/serilogrequestlogs"; + } + + public void Register(IAppHost appHost) + { + //Validator.ValidateAndThrow(this); + + appHost.GlobalRequestFiltersAsync.Add(RequestFilter); + appHost.GlobalResponseFilters.Add(ResponseFilter); + + var requestLogger = new RequestLogger(); + requestLogger = new FeatureConfig().ApplyAppSettings(requestLogger, appHost); + requestLogger.LogEntryPropertiesGenerator = LogEntryPropertiesGenerator; + requestLogger.RequiredRoles = RequiredRoles; + requestLogger.SkipLogging = SkipLogging; + requestLogger.ExcludeRequestDtoTypes = ExcludeRequestDtoTypes; + requestLogger.HideRequestBodyForRequestDtoTypes = HideRequestBodyForRequestDtoTypes; + appHost.Register(requestLogger); + + appHost.RegisterService(AtRestPath); + + appHost.GetPlugin() + .AddDebugLink(AtRestPath, "Serilog Request Logs"); + } + + private Task RequestFilter(IRequest request, IResponse response, object dto) + { + if (request.Items.ContainsKey(SerilogRequestLogsLoggerKey)) + request.Items.Remove(SerilogRequestLogsLoggerKey); + + request.Items.Add(SerilogRequestLogsLoggerKey, CreateSerilogLogger()); + + return Task.CompletedTask; + } + + private void ResponseFilter(IRequest request, IResponse response, object dto) + { + if (request.Items.ContainsKey(SerilogRequestLogsLoggerKey)) + { + var logger = request.Items[SerilogRequestLogsLoggerKey] as IDisposable; + logger?.Dispose(); + + request.Items.Remove(SerilogRequestLogsLoggerKey); + } + } + + private ILogger CreateSerilogLogger() + { + var loggerConfiguration = new LoggerConfiguration() + .MinimumLevel.Information(); + + if (SerilogLoggerBuilder != null) + loggerConfiguration = SerilogLoggerBuilder(loggerConfiguration); + + return SerilogLoggerFactory != null + ? SerilogLoggerFactory.Invoke() + : loggerConfiguration.CreateLogger() + ; + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureConfig.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureConfig.cs new file mode 100644 index 0000000..4704201 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureConfig.cs @@ -0,0 +1,29 @@ +using ServiceStack.Configuration; +using ServiceStack.Serilog.RequestLogsFeature.Logging; + +namespace ServiceStack.Serilog.RequestLogsFeature.Plugin +{ + public class FeatureConfig + { + public const string EnableSessionTrackingKey = "serilogrequestlogs:EnableSessionTracking"; + public const string EnableRequestBodyTrackingKey = "serilogrequestlogs:EnableRequestBodyTracking"; + public const string EnableResponseTrackingKey = "serilogrequestlogs:EnableResponseTracking"; + public const string EnableErrorTrackingKey = "serilogrequestlogs:EnableErrorTracking"; + public const string LimitToServiceRequestsKey = "serilogrequestlogs:LimitToServiceRequests"; + + public RequestLogger ApplyAppSettings(RequestLogger logger, IAppHost appHost) + { + if (logger == null) + return logger; + + var appSettings = new AppSettings(); + logger.EnableSessionTracking = appSettings.Get(EnableSessionTrackingKey); + logger.EnableRequestBodyTracking = appSettings.Get(EnableRequestBodyTrackingKey); + logger.EnableResponseTracking = appSettings.Get(EnableResponseTrackingKey); + logger.EnableErrorTracking = appSettings.Get(EnableErrorTrackingKey); + logger.LimitToServiceRequests = appSettings.Get(LimitToServiceRequestsKey); + + return logger; + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureService.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureService.cs new file mode 100644 index 0000000..3caddba --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureService.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using ServiceStack.DataAnnotations; +using ServiceStack.Web; + +namespace ServiceStack.Serilog.RequestLogsFeature.Plugin +{ + [Exclude(ServiceStack.Feature.Soap)] + [DataContract] + public class SerilogRequestLogs + { + [DataMember(Order = 1)] public bool? EnableSessionTracking { get; set; } + [DataMember(Order = 2)] public bool? EnableRequestBodyTracking { get; set; } + [DataMember(Order = 3)] public bool? EnableResponseTracking { get; set; } + [DataMember(Order = 4)] public bool? EnableErrorTracking { get; set; } + [DataMember(Order = 5)] public bool? LimitToServiceRequests { get; set; } + [DataMember(Order = 6)] public int Skip { get; set; } + [DataMember(Order = 7)] public int? Take { get; set; } + } + + [Exclude(ServiceStack.Feature.Soap)] + [DataContract] + public class SerilogRequestLogsResponse + { + public SerilogRequestLogsResponse() + { + this.Results = new List(); + } + + [DataMember(Order = 1)] public List Results { get; set; } + [DataMember(Order = 2)] public Dictionary Usage { get; set; } + [DataMember(Order = 3)] public ResponseStatus ResponseStatus { get; set; } + } + + [DefaultRequest(typeof(SerilogRequestLogs))] + [Restrict(VisibilityTo = RequestAttributes.None)] + public class FeatureService : Service + { + private static readonly Dictionary Usage = new Dictionary { + ["bool EnableSessionTracking"] = "Turn On/Off Tracking of Session", + ["bool EnableRequestBodyTracking"] = "Turn On/Off Tracking of Request Body", + ["bool EnableResponseTracking"] = "Turn On/Off Tracking of Responses", + ["bool EnableErrorTracking"] = "Turn On/Off Tracking of Errors", + ["bool LimitToServiceRequests"] = "Turn On/Off Limiting of Service Requests", + ["int Skip"] = "Skip past N results", + ["int Take"] = "Only look at last N results" + }; + + public IRequestLogger RequestLogger { get; set; } + + public SerilogRequestLogsResponse Any(SerilogRequestLogs request) + { + if (RequestLogger == null) + throw new Exception("No IRequestLogger is registered"); + + if (!HostContext.DebugMode) + RequiredRoleAttribute.AssertRequiredRoles(Request, RequestLogger.RequiredRoles); + + if (request.EnableSessionTracking.HasValue) + RequestLogger.EnableSessionTracking = request.EnableSessionTracking.Value; + + if (request.EnableRequestBodyTracking.HasValue) + RequestLogger.EnableRequestBodyTracking = request.EnableRequestBodyTracking.Value; + + if (request.LimitToServiceRequests.HasValue) + RequestLogger.LimitToServiceRequests = request.LimitToServiceRequests.Value; + + if (request.EnableResponseTracking.HasValue) + RequestLogger.EnableResponseTracking = request.EnableResponseTracking.Value; + + if (request.EnableErrorTracking.HasValue) + RequestLogger.EnableErrorTracking = request.EnableErrorTracking.Value; + + var logs = RequestLogger + .GetLatestLogs(request.Take) + .Skip(request.Skip) + .OrderByDescending(x => x.Id) + .ToList(); + + return new SerilogRequestLogsResponse + { + Results = logs, + Usage = Usage + }; + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureValidator.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureValidator.cs new file mode 100644 index 0000000..da610ec --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Plugin/FeatureValidator.cs @@ -0,0 +1,14 @@ +using ServiceStack.FluentValidation; + +namespace ServiceStack.Serilog.RequestLogsFeature.Plugin +{ + public class FeatureValidator : AbstractValidator + { + public FeatureValidator() + { + RuleFor(feat => feat.RequestLogger) + .NotNull() + ; + } + } +} diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/Properties/AssemblyInfo.cs b/src/ServiceStack.Serilog.RequestLogsFeature/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7c0bf16 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ServiceStack.Serilog.RequestLogsFeature")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ServiceStack.Serilog.RequestLogsFeature")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1abef0bf-672e-4559-afc8-b28138ccb19b")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/ServiceStack.Serilog.RequestLogsFeature.csproj b/src/ServiceStack.Serilog.RequestLogsFeature/ServiceStack.Serilog.RequestLogsFeature.csproj new file mode 100644 index 0000000..5bae9b7 --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/ServiceStack.Serilog.RequestLogsFeature.csproj @@ -0,0 +1,79 @@ + + + + + Debug + AnyCPU + {1ABEF0BF-672E-4559-AFC8-B28138CCB19B} + Library + Properties + ServiceStack.Serilog.RequestLogsFeature + ServiceStack.Serilog.RequestLogsFeature + v4.6.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Serilog.2.5.0\lib\net46\Serilog.dll + + + ..\packages\ServiceStack.4.5.14\lib\net45\ServiceStack.dll + + + ..\packages\ServiceStack.Client.4.5.14\lib\net45\ServiceStack.Client.dll + + + ..\packages\ServiceStack.Common.4.5.14\lib\net45\ServiceStack.Common.dll + + + ..\packages\ServiceStack.Interfaces.4.5.14\lib\portable-wp80+sl5+net45+win8+wpa81+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll + + + ..\packages\ServiceStack.Logging.Serilog.4.5.14\lib\net45\ServiceStack.Logging.Serilog.dll + + + ..\packages\ServiceStack.Text.4.5.14\lib\net45\ServiceStack.Text.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ServiceStack.Serilog.RequestLogsFeature/packages.config b/src/ServiceStack.Serilog.RequestLogsFeature/packages.config new file mode 100644 index 0000000..c826f9a --- /dev/null +++ b/src/ServiceStack.Serilog.RequestLogsFeature/packages.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file