mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
overhauling logging to make it better
This commit is contained in:
@@ -10,7 +10,7 @@ dotnet_naming_symbols.private_methods.applicable_kinds = method
|
|||||||
dotnet_naming_symbols.private_methods.applicable_accessibilities = private
|
dotnet_naming_symbols.private_methods.applicable_accessibilities = private
|
||||||
|
|
||||||
dotnet_naming_style.camel_case_style.capitalization = camel_case
|
dotnet_naming_style.camel_case_style.capitalization = camel_case
|
||||||
|
dotnet_diagnostic.CA2254.severity = none
|
||||||
|
|
||||||
[*.razor]
|
[*.razor]
|
||||||
indent_style = ignore
|
indent_style = ignore
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ global using System.Text.Json.Serialization;
|
|||||||
global using System.Text.Json;
|
global using System.Text.Json;
|
||||||
global using System.ComponentModel.DataAnnotations;
|
global using System.ComponentModel.DataAnnotations;
|
||||||
global using Management.Services.Canvas;
|
global using Management.Services.Canvas;
|
||||||
|
global using Management.Services;
|
||||||
global using CanvasModel.EnrollmentTerms;
|
global using CanvasModel.EnrollmentTerms;
|
||||||
global using CanvasModel.Courses;
|
global using CanvasModel.Courses;
|
||||||
global using CanvasModel;
|
global using CanvasModel;
|
||||||
@@ -32,6 +33,9 @@ if (canvas_url == null)
|
|||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddServerSideBlazor();
|
builder.Services.AddServerSideBlazor();
|
||||||
|
|
||||||
|
builder.Services.AddLogging();
|
||||||
|
builder.Services.AddScoped(typeof(MyLogger<>));
|
||||||
|
|
||||||
builder.Services.AddScoped<IWebRequestor, WebRequestor>();
|
builder.Services.AddScoped<IWebRequestor, WebRequestor>();
|
||||||
builder.Services.AddScoped<CanvasServiceUtils>();
|
builder.Services.AddScoped<CanvasServiceUtils>();
|
||||||
builder.Services.AddScoped<CanvasAssignmentService>();
|
builder.Services.AddScoped<CanvasAssignmentService>();
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ public static partial class ModuleSyncronizationExtensions
|
|||||||
{
|
{
|
||||||
var canvasAssignmentId =
|
var canvasAssignmentId =
|
||||||
localQuiz.CanvasId
|
localQuiz.CanvasId
|
||||||
?? throw new Exception("cannot create module item if assignment does not have canvas id");
|
?? throw new Exception("cannot create module item if quiz does not have canvas id");
|
||||||
await canvas.CreateModuleItem(
|
await canvas.CreateModuleItem(
|
||||||
canvasId,
|
canvasId,
|
||||||
moduleCanvasId,
|
moduleCanvasId,
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
global using System.Text.Json.Serialization;
|
global using System.Text.Json.Serialization;
|
||||||
global using System.Text.Json;
|
global using System.Text.Json;
|
||||||
|
global using Microsoft.Extensions.Logging;
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Markdig" Version="0.31.0" />
|
<PackageReference Include="Markdig" Version="0.31.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||||
<PackageReference Include="RestSharp" Version="108.0.3" />
|
<PackageReference Include="RestSharp" Version="108.0.3" />
|
||||||
<PackageReference Include="YamlDotNet" Version="13.1.1" />
|
<PackageReference Include="YamlDotNet" Version="13.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -8,11 +8,17 @@ public class CanvasAssignmentGroupService
|
|||||||
{
|
{
|
||||||
private readonly IWebRequestor webRequestor;
|
private readonly IWebRequestor webRequestor;
|
||||||
private readonly CanvasServiceUtils utils;
|
private readonly CanvasServiceUtils utils;
|
||||||
|
private readonly ILogger<CanvasAssignmentGroupService> log;
|
||||||
|
|
||||||
public CanvasAssignmentGroupService(IWebRequestor webRequestor, CanvasServiceUtils utils)
|
public CanvasAssignmentGroupService(
|
||||||
|
IWebRequestor webRequestor,
|
||||||
|
CanvasServiceUtils utils,
|
||||||
|
ILogger<CanvasAssignmentGroupService> logger
|
||||||
|
)
|
||||||
{
|
{
|
||||||
this.webRequestor = webRequestor;
|
this.webRequestor = webRequestor;
|
||||||
this.utils = utils;
|
this.utils = utils;
|
||||||
|
this.log = logger;
|
||||||
}
|
}
|
||||||
public async Task<IEnumerable<CanvasAssignmentGroup>> GetAll(ulong courseId)
|
public async Task<IEnumerable<CanvasAssignmentGroup>> GetAll(ulong courseId)
|
||||||
{
|
{
|
||||||
@@ -29,7 +35,7 @@ public class CanvasAssignmentGroupService
|
|||||||
LocalAssignmentGroup localAssignmentGroup
|
LocalAssignmentGroup localAssignmentGroup
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"creating assignment group: {localAssignmentGroup.Name}");
|
log.LogInformation($"creating assignment group: {localAssignmentGroup.Name}");
|
||||||
var url = $"courses/{canvasCourseId}/assignment_groups";
|
var url = $"courses/{canvasCourseId}/assignment_groups";
|
||||||
var request = new RestRequest(url);
|
var request = new RestRequest(url);
|
||||||
var body = new
|
var body = new
|
||||||
@@ -53,7 +59,7 @@ public class CanvasAssignmentGroupService
|
|||||||
LocalAssignmentGroup localAssignmentGroup
|
LocalAssignmentGroup localAssignmentGroup
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"updating assignment group: {localAssignmentGroup.Name}");
|
log.LogInformation($"updating assignment group: {localAssignmentGroup.Name}");
|
||||||
if (localAssignmentGroup.CanvasId == null)
|
if (localAssignmentGroup.CanvasId == null)
|
||||||
throw new Exception("cannot update assignment group if canvas id is null");
|
throw new Exception("cannot update assignment group if canvas id is null");
|
||||||
var url = $"courses/{canvasCourseId}/assignment_groups/{localAssignmentGroup.CanvasId}";
|
var url = $"courses/{canvasCourseId}/assignment_groups/{localAssignmentGroup.CanvasId}";
|
||||||
|
|||||||
@@ -8,11 +8,17 @@ public class CanvasAssignmentService
|
|||||||
{
|
{
|
||||||
private readonly IWebRequestor webRequestor;
|
private readonly IWebRequestor webRequestor;
|
||||||
private readonly CanvasServiceUtils utils;
|
private readonly CanvasServiceUtils utils;
|
||||||
|
private readonly MyLogger<CanvasAssignmentService> log;
|
||||||
|
|
||||||
public CanvasAssignmentService(IWebRequestor webRequestor, CanvasServiceUtils utils)
|
public CanvasAssignmentService(
|
||||||
|
IWebRequestor webRequestor,
|
||||||
|
CanvasServiceUtils utils,
|
||||||
|
MyLogger<CanvasAssignmentService> logger
|
||||||
|
)
|
||||||
{
|
{
|
||||||
this.webRequestor = webRequestor;
|
this.webRequestor = webRequestor;
|
||||||
this.utils = utils;
|
this.utils = utils;
|
||||||
|
this.log = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<CanvasAssignment>> GetAll(ulong courseId)
|
public async Task<IEnumerable<CanvasAssignment>> GetAll(ulong courseId)
|
||||||
@@ -36,7 +42,7 @@ public class CanvasAssignmentService
|
|||||||
ulong? canvasAssignmentGroupId
|
ulong? canvasAssignmentGroupId
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"creating assignment: {localAssignment.Name}");
|
log.Log($"creating assignment: {localAssignment.Name}");
|
||||||
var url = $"courses/{canvasCourseId}/assignments";
|
var url = $"courses/{canvasCourseId}/assignments";
|
||||||
var request = new RestRequest(url);
|
var request = new RestRequest(url);
|
||||||
var body = new
|
var body = new
|
||||||
@@ -69,7 +75,7 @@ public class CanvasAssignmentService
|
|||||||
ulong? canvasAssignmentGroupId
|
ulong? canvasAssignmentGroupId
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"updating assignment: {localAssignment.Name}");
|
log.Log($"updating assignment: {localAssignment.Name}");
|
||||||
var url = $"courses/{courseId}/assignments/{localAssignment.CanvasId}";
|
var url = $"courses/{courseId}/assignments/{localAssignment.CanvasId}";
|
||||||
var request = new RestRequest(url);
|
var request = new RestRequest(url);
|
||||||
var body = new
|
var body = new
|
||||||
@@ -84,8 +90,7 @@ public class CanvasAssignmentService
|
|||||||
};
|
};
|
||||||
var bodyObj = new { assignment = body };
|
var bodyObj = new { assignment = body };
|
||||||
request.AddBody(bodyObj);
|
request.AddBody(bodyObj);
|
||||||
Console.WriteLine(url);
|
|
||||||
Console.WriteLine(JsonSerializer.Serialize(bodyObj));
|
|
||||||
await webRequestor.PutAsync(request);
|
await webRequestor.PutAsync(request);
|
||||||
|
|
||||||
await CreateRubric(courseId, localAssignment);
|
await CreateRubric(courseId, localAssignment);
|
||||||
@@ -93,13 +98,13 @@ public class CanvasAssignmentService
|
|||||||
|
|
||||||
public async Task Delete(ulong courseId, LocalAssignment assignment)
|
public async Task Delete(ulong courseId, LocalAssignment assignment)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"deleting assignment from canvas {assignment.Name}");
|
log.Log($"deleting assignment from canvas {assignment.Name}");
|
||||||
var url = $"courses/{courseId}/assignments/{assignment.CanvasId}";
|
var url = $"courses/{courseId}/assignments/{assignment.CanvasId}";
|
||||||
var request = new RestRequest(url);
|
var request = new RestRequest(url);
|
||||||
var response = await webRequestor.DeleteAsync(request);
|
var response = await webRequestor.DeleteAsync(request);
|
||||||
if (!response.IsSuccessful)
|
if (!response.IsSuccessful)
|
||||||
{
|
{
|
||||||
Console.WriteLine(url);
|
log.Log(url);
|
||||||
throw new Exception("Failed to delete assignment");
|
throw new Exception("Failed to delete assignment");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using CanvasModel;
|
using CanvasModel;
|
||||||
using CanvasModel.Assignments;
|
using CanvasModel.Assignments;
|
||||||
using CanvasModel.Courses;
|
using CanvasModel.Courses;
|
||||||
@@ -11,6 +12,7 @@ public class CanvasService
|
|||||||
{
|
{
|
||||||
private readonly IWebRequestor webRequestor;
|
private readonly IWebRequestor webRequestor;
|
||||||
private readonly CanvasServiceUtils utils;
|
private readonly CanvasServiceUtils utils;
|
||||||
|
private readonly ILogger<CanvasService> logger;
|
||||||
|
|
||||||
public CanvasAssignmentService Assignments { get; }
|
public CanvasAssignmentService Assignments { get; }
|
||||||
public CanvasAssignmentGroupService AssignmentGroups { get; }
|
public CanvasAssignmentGroupService AssignmentGroups { get; }
|
||||||
@@ -23,7 +25,8 @@ public class CanvasService
|
|||||||
CanvasAssignmentService Assignments,
|
CanvasAssignmentService Assignments,
|
||||||
CanvasAssignmentGroupService AssignmentGroups,
|
CanvasAssignmentGroupService AssignmentGroups,
|
||||||
CanvasModuleService Modules,
|
CanvasModuleService Modules,
|
||||||
CanvasQuizService Quizzes
|
CanvasQuizService Quizzes,
|
||||||
|
ILogger<CanvasService> logger
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.webRequestor = webRequestor;
|
this.webRequestor = webRequestor;
|
||||||
@@ -32,6 +35,7 @@ public class CanvasService
|
|||||||
this.AssignmentGroups = AssignmentGroups;
|
this.AssignmentGroups = AssignmentGroups;
|
||||||
this.Modules = Modules;
|
this.Modules = Modules;
|
||||||
this.Quizzes = Quizzes;
|
this.Quizzes = Quizzes;
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<EnrollmentTermModel>> GetTerms()
|
public async Task<IEnumerable<EnrollmentTermModel>> GetTerms()
|
||||||
@@ -60,8 +64,8 @@ public class CanvasService
|
|||||||
|
|
||||||
if (data == null)
|
if (data == null)
|
||||||
{
|
{
|
||||||
Console.WriteLine(response.Content);
|
logger.LogError(response.Content);
|
||||||
Console.WriteLine(response.ResponseUri);
|
logger.LogError(response.ResponseUri?.ToString());
|
||||||
throw new Exception("error getting course from canvas");
|
throw new Exception("error getting course from canvas");
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
@@ -91,7 +95,7 @@ public class CanvasService
|
|||||||
CanvasModuleItem item
|
CanvasModuleItem item
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"updating module item {item.Title}");
|
logger.LogInformation($"updating module item {item.Title}");
|
||||||
var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items/{item.Id}";
|
var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items/{item.Id}";
|
||||||
var body = new { module_item = new { title = item.Title, position = item.Position } };
|
var body = new { module_item = new { title = item.Title, position = item.Position } };
|
||||||
var request = new RestRequest(url);
|
var request = new RestRequest(url);
|
||||||
@@ -110,7 +114,7 @@ public class CanvasService
|
|||||||
ulong contentId
|
ulong contentId
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"creating new module item {title}");
|
logger.LogInformation($"creating new module item {title}");
|
||||||
var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items";
|
var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items";
|
||||||
var body = new
|
var body = new
|
||||||
{
|
{
|
||||||
|
|||||||
26
Management/Services/Logger.cs
Normal file
26
Management/Services/Logger.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Management.Services;
|
||||||
|
|
||||||
|
|
||||||
|
public class MyLogger<T>
|
||||||
|
{
|
||||||
|
private readonly ILogger<T> baseLogger;
|
||||||
|
|
||||||
|
public MyLogger(ILogger<T> baseLogger)
|
||||||
|
{
|
||||||
|
this.baseLogger = baseLogger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Log(
|
||||||
|
string message,
|
||||||
|
LogLevel logLevel = LogLevel.Information,
|
||||||
|
[CallerMemberName] string memberName = ""
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var finalMessage = $"[{typeof(T)}.{memberName}] {message}";
|
||||||
|
|
||||||
|
// baseLogger.Log(logLevel, finalMessage);
|
||||||
|
Console.WriteLine(finalMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user