Created
November 27, 2023 13:59
-
-
Save MichalBrylka/eb01a38536d908c953c29a629b8a1fef to your computer and use it in GitHub Desktop.
Interceptor for logging
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.Runtime.CompilerServices; | |
namespace Net8.Features; | |
[Order(9)] | |
internal class InterceptorsLogging : IShowable | |
{ | |
public void Show() | |
{ | |
var todo = new TodoApi(); | |
todo.AddTodo("feed birds"); | |
var id = todo.AddTodo("wash dishes"); | |
todo.AddTodo("do laundry"); | |
var task = todo.GetTodo(id); | |
todo.RemoveTodo(id); | |
task = todo.GetTodo(id); | |
/*OUTPUT: | |
Method AddTodo started with params: feed birds | |
Method AddTodo finished in 00:00:00.0005184 | |
Data: (1. feed birds) | |
Method AddTodo started with params: wash dishes | |
Method AddTodo finished in 00:00:00.0008484 | |
Data: (1. feed birds, 2. wash dishes) | |
Method AddTodo started with params: do laundry | |
Method AddTodo finished in 00:00:00.0001032 | |
Data: (1. feed birds, 2. wash dishes, 3. do laundry) | |
Method GetTodo started with params: 2 | |
Method GetTodo finished in 00:00:00.0004710 | |
Data: (1. feed birds, 2. wash dishes, 3. do laundry) | |
Method RemoveTodo started with params: 2 | |
Method RemoveTodo finished in 00:00:00.0002863 | |
Data: (1. feed birds, 3. do laundry) | |
Method GetTodo started with params: 2 | |
Method GetTodo finished in 00:00:00.0000454 | |
Data: (1. feed birds, 3. do laundry) | |
*/ | |
} | |
} | |
class TodoApi | |
{ | |
private readonly List<(int Id, string Task)> _data = []; | |
public int AddTodo(string task) | |
{ | |
var id = _data.Count == 0 ? 1 : _data.Max(x => x.Id) + 1; | |
_data.Add((id, task)); | |
return id; | |
} | |
public string? GetTodo(int id) => | |
_data.FirstOrDefault(t => t.Id == id).Task; | |
public void RemoveTodo(int id) | |
{ | |
for (int i = _data.Count - 1; i >= 0; i--) | |
{ | |
if (_data[i].Id == id) | |
{ | |
_data.RemoveAt(i); | |
return; | |
} | |
} | |
} | |
public void PrintData() => | |
Console.WriteLine($"\tData: ({string.Join(", ", _data.Select(t => $"{t.Id}. {t.Task}"))})"); | |
} | |
[Auto.AutoFilePath] | |
static partial class TodoApiInterceptor | |
{ | |
[InterceptsLocation(file, line: 11, character: 14)] | |
[InterceptsLocation(file, line: 12, character: 23)] | |
[InterceptsLocation(file, line: 13, character: 14)] | |
public static int AddTodoInterceptor(this TodoApi todo, string task) | |
{ | |
using var _ = new ForeColor(ConsoleColor.Blue); | |
var sw = System.Diagnostics.Stopwatch.StartNew(); | |
Console.WriteLine($"Method {nameof(TodoApi.AddTodo)} started with params: {task}"); | |
try | |
{ | |
return todo.AddTodo(task); | |
} | |
finally | |
{ | |
sw.Stop(); | |
Console.WriteLine($"Method {nameof(TodoApi.AddTodo)} finished in {sw.Elapsed}"); | |
todo.PrintData(); | |
} | |
} | |
[InterceptsLocation(file, line: 15, character: 25)] | |
[InterceptsLocation(file, line: 18, character: 21)] | |
public static string? GetTodoInterceptor(this TodoApi todo, int id) | |
{ | |
using var _ = new ForeColor(ConsoleColor.Red); | |
var sw = System.Diagnostics.Stopwatch.StartNew(); | |
Console.WriteLine($"Method {nameof(TodoApi.GetTodo)} started with params: {id}"); | |
try | |
{ | |
return todo.GetTodo(id); | |
} | |
finally | |
{ | |
sw.Stop(); | |
Console.WriteLine($"Method {nameof(TodoApi.GetTodo)} finished in {sw.Elapsed}"); | |
todo.PrintData(); | |
} | |
} | |
[InterceptsLocation(file, line: 17, character: 14)] | |
public static void RemoveTodoInterceptor(this TodoApi todo, int id) | |
{ | |
using var _ = new ForeColor(ConsoleColor.Green); | |
var sw = System.Diagnostics.Stopwatch.StartNew(); | |
Console.WriteLine($"Method {nameof(TodoApi.RemoveTodo)} started with params: {id}"); | |
try | |
{ | |
todo.RemoveTodo(id); | |
} | |
finally | |
{ | |
sw.Stop(); | |
Console.WriteLine($"Method {nameof(TodoApi.RemoveTodo)} finished in {sw.Elapsed}"); | |
todo.PrintData(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment