How to get the name of the current class in C# (own class name)?
Hello everyone! I'm a beginner C# developer and I've run into this problem. I'm writing a small logger for my application, and I need each log message to automatically include the name of the class from which the logging method was called.\n\nI'm trying different approaches, but nothing works as expected. Here's my code:\n\n```csharp\npublic class MyClass\n{\n public void DoSomething()\n {\n Logger.Log("Starting operation");\n // ... some code\n }\n}\n\npublic static class Logger\n{\n public static void Log(string message)\n {\n // I want to get "MyClass" here\n string className = ???? ; // what to put here?\n Console.WriteLine($"[{className}] {message}");\n }\n}\n```\n\nI tried using `typeof(MyClass).Name`, but that hardcodes the class na
No answers yet.
Log in to leave an answer
Blogs
Error Handling in JavaScript: try/catch and Advanced Techniques in Node.js
Iterating over a string in Rust: how to traverse characters without pain
Monitoring and Logging in Rust: From Chaos to Order
Mutation Testing in TypeScript: Breaking Code to Make It Better