简体   繁体   English

如何像在 Unity 中一样制作可点击的 C# 控制台 output 线?

[英]How to make a clickable C# Console output line like in Unity?

Unity Game Engine has a very useful feature in its console, where you can click on any line in the output, and it will take you to the line of source code that produced it. Unity Game Engine 在其控制台中有一个非常有用的功能,您可以在其中单击 output 中的任何一行,它会将您带到生成它的源代码行。

For example例如

Debug.Log("test"); (line 20 in Test.cs) (Test.cs 中的第 20 行)

will output将 output

test

double clicking on test, will bring you back to line 20 in Test.cs in the IDE.双击测试,将带您回到 IDE 的 Test.cs 中的第 20 行。

I was wondering, is there some way to do this in normal C# development?我想知道,在正常的 C# 开发中是否有办法做到这一点? I'm using Rider IDE, but I suspect this is some kind of hyperlink rather than something IDE dependent.我正在使用 Rider IDE,但我怀疑这是某种超链接,而不是依赖于 IDE 的东西。 Although perhaps that's not accurate.虽然也许这并不准确。

Unfortunately, the current Windows Terminal does not support clickable links.不幸的是,当前的 Windows 终端不支持可点击链接。 However, it is a planned feature for WT 2.0 .但是,它是WT 2.0 的计划功能 Read here for the pull request.在此处阅读拉取请求。 You may be able to build their dev branch if you are just doing a project for yourself.如果您只是为自己做一个项目,您也许可以建立他们的开发分支。

Right now, you can obviously open your ide with arguments but I think that's your only option.现在,你显然可以用 arguments 打开你的 ide 但我认为这是你唯一的选择。

I managed to figure this out pretty well.我设法很好地解决了这个问题。 This piggybacks on Jetbrains IDEs parsing the console, and it creates links to paths.这搭载 Jetbrains IDE 解析控制台,并创建路径链接。 I'm not sure this will work in other IDEs, but it actually works super well for what I wanted.我不确定这是否适用于其他 IDE,但它实际上非常适合我想要的。

const string ProjectFolder = "MyProject/";
    
public static void Log(string message,
    [CallerMemberName] string callingMethod = "",
    [CallerFilePath] string callingFilePath = "",
    [CallerLineNumber] int callingFileLineNumber = 0)
{

    string pathFromProjectFolder = callingFilePath.Split(ProjectFolder).LastOrDefault();
    Console.WriteLine($"{message}   {pathFromProjectFolder}:{callingFileLineNumber} ");
}

Calling this method produces something like this in the console for Rider.调用此方法会在 Rider 的控制台中生成类似的内容。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM