admin管理员组文章数量:1434909
With NodeJS in VSCode all console.log
logs have a clickable link which takes you to the source code line where the log was generated:
How can we achieve the same result with csharp?
The goal should be that each log entry is clickable to take one to the source code location.
With NodeJS in VSCode all console.log
logs have a clickable link which takes you to the source code line where the log was generated:
How can we achieve the same result with csharp?
The goal should be that each log entry is clickable to take one to the source code location.
Share Improve this question asked Nov 18, 2024 at 10:11 MarcMarc 14.5k14 gold badges93 silver badges113 bronze badges2 Answers
Reset to default 1I did some googling and didnt find a standard solution for that. Since I do not know how familiar you are with C#, I created the following example to demonstrate the wanted behaviour. First you want to use the ILoggerFactory to implement a custom logging instance. Then we will need a custom Log extension method which outputs the filepath including the filenumber. Visual Studio Code will then catch the link making it clickable.
using Microsoft.Extensions.Logging;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Abstractions;
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(options =>
{
options.FormatterName = "lineLogger";
})
.AddConsoleFormatter<CustomFormatter, ConsoleFormatterOptions>();
});
var _logger = loggerFactory.CreateLogger<Program>();
_logger.LogWithLocation("Test Log");
public class CustomFormatter : ConsoleFormatter
{
public CustomFormatter() : base("lineLogger") { }
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter)
{
textWriter.WriteLine($"{logEntry.Formatter(logEntry.State, logEntry.Exception)}");
}
}
public static class LoggerExtensions
{
public static void LogWithLocation(this ILogger logger, string message, [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
logger.LogInformation($"{message} ({filePath}:{lineNumber})");
}
}
as a side note, I got an error saying that ILoggerFactory contains no definition for AddConsole. I found this answer 'ILoggerFactory' does not contain a definition for 'AddConsole' So simply add the package like this:
dotnet add package Microsoft.Extensions.Logging.Console
You will also need the Microsoft.Extensions.Logging + Abstractions packages. I also created a fiddle to show you how it works with the classical top level statements here https://dotnetfiddle/q680Vj
edit: I changed the implementation to an extension method of the ILoggerFactory which makes the method reusable across every instance of an ILogger instance. Also included using statements.
To help clarify @Aaron's answer and provide an example which works on https://dotnetfiddle/:
using System.IO;
using Microsoft.Extensions.Logging;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Abstractions;
namespace MyProject;
class Program
{
static void Main(string[] args)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(options =>
{
options.FormatterName = "lineLogger";
}).AddConsoleFormatter<CustomFormatter, ConsoleFormatterOptions>();
});
var _logger = loggerFactory.CreateLogger<Program>();
_logger.LogWithLocation("Test Log");
}
}
public class CustomFormatter : ConsoleFormatter
{
public CustomFormatter() : base("lineLogger")
{
}
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter)
{
textWriter.WriteLine($"{logEntry.Formatter(logEntry.State, logEntry.Exception)}");
}
}
public static class LoggerExtensions
{
public static void LogWithLocation(this ILogger logger, string message, [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
logger.LogInformation($"{message} ({filePath}:{lineNumber})");
}
}
Obviously on dotnetfiddle there are no filenames so the output is something like:
Test Log (:20)
But typically you would have the filename:
Test Log (C:\temp\Program.cs:20)
本文标签: Click Log to view C Source Code in VS CodeStack Overflow
版权声明:本文标题:Click Log to view C# Source Code in VS Code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627293a2667051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论