Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Assignment2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bad
Assignment2
Commits
074a6e32
Commit
074a6e32
authored
1 week ago
by
Nikoline Vistoft Bartholin
Browse files
Options
Downloads
Patches
Plain Diff
Changes in logging middleware
parent
976a6013
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
LoggerMiddleware.cs
+11
-6
11 additions, 6 deletions
LoggerMiddleware.cs
Program.cs
+1
-1
1 addition, 1 deletion
Program.cs
appsettings.json
+1
-1
1 addition, 1 deletion
appsettings.json
with
13 additions
and
8 deletions
LoggerMiddleware.cs
+
11
−
6
View file @
074a6e32
using
System.Diagnostics
;
using
assignment2.Interfaces
;
using
assignment2.Models
;
public
sealed
class
LoggerMiddleware
{
private
readonly
RequestDelegate
_next
;
private
readonly
ILogger
_logger
;
private
readonly
ILogService
_logService
;
public
LoggerMiddleware
(
RequestDelegate
next
,
ILogger
<
LoggerMiddleware
>
logger
)
public
LoggerMiddleware
(
RequestDelegate
next
,
ILogger
<
LoggerMiddleware
>
logger
,
ILogService
logService
)
{
_next
=
next
;
_logger
=
logger
;
_logService
=
logService
;
}
public
async
Task
Invoke
(
HttpContext
context
)
...
...
@@ -22,13 +26,14 @@ public sealed class LoggerMiddleware
// Add other details as needed
};
// Optionally, add user info if available.
if
(
context
.
User
?.
Identity
?.
IsAuthenticated
==
true
)
var
newLog
=
new
Log
{
requestDetails
[
"UserName"
]
=
context
.
User
?.
Identity
?.
Name
;
}
User
=
context
.
User
?.
Identity
?.
Name
??
"Anonymous"
,
Operation
=
context
.
Request
.
Method
,
Timestamp
=
DateTime
.
UtcNow
};
_logger
.
LogInformation
(
"Request completed with details: {@
RequestDetails}"
,
requestDetails
);
_logger
.
LogInformation
(
"Request completed with details: {@
newLog}"
,
newLog
);
await
_next
(
context
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Program.cs
+
1
−
1
View file @
074a6e32
...
...
@@ -22,7 +22,7 @@ builder.Services.AddTransient<IExperienceService, ExperienceService>();
builder
.
Services
.
AddTransient
<
IProviderService
,
ProviderService
>();
builder
.
Services
.
AddTransient
<
ISharedExperienceService
,
SharedExperienceService
>();
builder
.
Services
.
AddTransient
<
IGuestService
,
GuestService
>();
builder
.
Services
.
AddS
coped
<
ILogService
,
LogsService
>();
builder
.
Services
.
AddS
ingleton
<
ILogService
,
LogsService
>();
Env
.
Load
();
builder
.
Services
.
AddDbContext
<
ExperienceDBContext
>();
builder
.
Services
.
AddFusionCache
().
WithDefaultEntryOptions
(
new
FusionCacheEntryOptions
...
...
This diff is collapsed.
Click to expand it.
appsettings.json
+
1
−
1
View file @
074a6e32
{
"
BookStoreDatabase
"
:
{
"
LoggingDB
"
:
{
"ConnectionString"
:
"mongodb://localhost:27017"
,
"DatabaseName"
:
"AssignmentLoggingDB"
,
"LogsCollectionName"
:
"Logs"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment