Skip to content

Commit

Permalink
Rework: Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonAquitaine committed Nov 26, 2023
1 parent e64391c commit ab3f830
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Zen.Web/Middleware/Html5Router.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -65,10 +64,10 @@ public static void UseHtml5Routing(this IApplicationBuilder app)

var physicalPath = Path.Combine(pathParts.ToArray());

Base.Log.Add(context.Request.Path + context.Request.QueryString, Base.Module.Log.Message.EContentType.Debug);
Base.Log.Add(physicalPath, Base.Module.Log.Message.EContentType.Debug);
//Base.Log.Add(context.Request.Path + context.Request.QueryString, Base.Module.Log.Message.EContentType.Debug);
//Base.Log.Add(physicalPath, Base.Module.Log.Message.EContentType.Debug);

await LogRequest(context);
await LogAnalytics(context);

if (File.Exists(physicalPath)) { await next.Invoke(); return; }

Expand All @@ -92,7 +91,7 @@ public static void UseHtml5Routing(this IApplicationBuilder app)
});
}

private static async Task LogRequest(HttpContext context)
private static async Task LogAnalytics(HttpContext context)
{
var url = context.Request.GetDisplayUrl();

Expand All @@ -102,7 +101,13 @@ private static async Task LogRequest(HttpContext context)

try
{
new RequestLog() { Referer = context.Request.Headers["referer"].ToString(), Url = context.Request.GetDisplayUrl(), }.Save();
new Model.Analytics.Request()
{
Url = context.Request.GetDisplayUrl(),
Headers = context.Request.Headers.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString()),
Path = context.Request.Path + context.Request.QueryString,
Type = Model.Analytics.Request.EType.Html5Redirect
}.Save();
}
catch (Exception) { }

Expand Down
27 changes: 27 additions & 0 deletions Zen.Web/Model/Analytics/Request.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Zen.Base.Module;
using Zen.Base.Module.Data.CommonAttributes;

namespace Zen.Web.Model.Analytics
{
internal class Request : Data<Request>, IDataId
{
public enum EType
{
Regular,
Html5Redirect
}


[Key]
public string Id { get; set; }
[Display]
public string Url { get; set; }
public Dictionary<string, string> Headers { get; set; }
public string Path { get; set; }
public EType Type { get; set; } = EType.Regular;
public DateTime Timestamp { get; set; } = DateTime.Now;
}
}
3 changes: 3 additions & 0 deletions Zen.Web/Model/RequestLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Zen.Base.Module;
using Zen.Base.Module.Data.CommonAttributes;
Expand All @@ -16,5 +17,7 @@ public class RequestLog : Data<RequestLog>, IDataId
public string Referer { get; set; }

public DateTime Timestamp { get; set; } = DateTime.Now;
public Dictionary<string, string> Headers { get; internal set; }
public string Path { get; internal set; }
}
}

0 comments on commit ab3f830

Please sign in to comment.