Is there a way to use hyperlink in Unity's console? - c#

I'm using a custom logger(log4net) for Unity, following this guide. This involves making a custom appender that writes to Unity's console using Debug.Log(). All of this works great, however the problem is that whenever a log statement is printed it originates from the custom appender. So when I double click the message in the console I end up opening the code for the appender and not the source code from where the message originated. I want to print a clickable hyperlink in the console to the path of the source code.(I already have the path)
It seems the Unity console already supports clickable links:
I just don't know how to make it print links that I want.
In IDE's such as Intellij the console automatically parses links,so I tried to print the path for the source in the unity console for example:
(at Assets/Scripts/MovablePlatform.cs:)
But this didn't work
TL;DR
I want to know if it's possible to print clickable link in Unity's console such as 'http://www.google.com' or 'file:///D:/Mysourcode/main.cs'

Rich text supports anchor tags for file paths, but it doesn't appear to support URLs. Your example would be:
Debug.Log("Assets/Scripts/MovablePlatform.cs:7");

Logging a link like asset works for creating asset links since Unity 2019. This is the same mechanism used internally by Unity for clickable stack traces (also introduced in that version). The path must reference an existing Unity asset (not just any file).
Note, however, that while the link renders in blue in the main list of debug log messages, it isn't clickable there, only in the detailed pane below (which also shows the stack trace). That is, you must first click the log message, then click the link in the detailed message pane.
(Finally, in my testing I occasionally hit what may be a Unity bug or a bug in the Rider IDE integration, where I sometimes have to Alt+Tab away from Unity and back before it will open any asset link, stack trace links included. I assume doing so resets some sort of state in Unity's asset database, which is why it fixes things.)
Since Unity 2021.2.0a11, both web links and file links are supported:
Debug.Log("Click me: local file");
Debug.Log("Click me: website");
You can now even hook into the hyperlink handling and do whatever you please with the result using EditorGUI.hyperLinkClicked.
UnityEditor.EditorGUI.hyperLinkClicked += (window, args)
=> Debug.Log($"clicked link to {args.hyperLinkData["href"]} in {window}");
so you can even implement your own ad-hoc links (by using other attributes than href, to avoid clashes).

As far as I know, no.
Those links are there so that you can jump to any of the calling methods in the stack trace that resulted in the Debug.Log call, and all you have to do is scroll down. Unity added this feature precisely because of log appenders but the system is entirely managed by Unity. Double-click is still going to go to the actual Debug.Log line, but you can still access other lines via those "hyperlinks" but they are built automagically by Unity and there is no API for writing those links yourself.
If you don't want your log appender to lose that information, don't do whatever it is that is making Unity lose that information when you use your log appender. You didn't include your code, so why the extra information is being lost is unclear. Also check your console settings and that you are using Unity 2019.1 or newer.

An open source Unity Console plug-in is recommended to solve this problem. Click console-hyperlinks!

Related

Library for Logging in Xamarin and error reporting style in MetroLog

I am an iOS developer which has been asked to do development in Xamarin. I want to log the outputs in Xamarin.
I am trying to find a library for this. I found one library for this called MetroLog.
Link : https://github.com/onovotny/MetroLog
But the problem is I am getting blue coloured output for every log level.Like this:
I was expecting that the errors would be in red , warnings would be in orange, and others in green or blue or something, but I could not get the required output in the manner I thought I would. I am running sample project from their repo , which has source code as follows:
_log.Info("Information - We are about to do magic!");
_log.Warn("Warning!");
_log.Trace("Trace some data.");
_log.Error("Something bad happened at {0}", DateTime.Now);
_log.Fatal("Danger Will Robinson!");
Are there some changes that I need to make to the logging project when it has been added to my project?
Is there any other library which can solve my use case for logging ?
If not then how can I achieve similar and coloured option from MetroLog ? I am open to other options(open source project) as well.
MORE INFO or EXTENDED QUESTION:
As I am used to iOS development I used to use the following statements in order to log the information:
NSLog(#"%# %# started" ,[self class] ,NSStringFromSelector(_cmd));
NSLog(#"%# %# ends " ,[self class] ,NSStringFromSelector(_cmd));
I am expecting some sort of quick logging option like the one for iOS as shown above without including any library. Is that possible?( including the class name and the method name which is executing the code)
Thanks.
I have not use MetroLog but, in general, such libraries requires you to set a destination / endpoint / server for your logs. The default one is often the basic one offered by the OS (which is the one that Xamarin Studio will redirect) and that won't give you coloured output.
Quick Logging: Every string given to System.Console.WriteLine ends up calling NSLog on iOS. Since that's part of mscorlib.dll (SDK) there's nothing else you'll need to add to your project to use it.

visual c# button opens file without specifying drive

I work for an IT company where we all carry around flash drives that have our most used programs on them.In my spare time I am hoping to create a "main menu" item that is kind of a fun and convenient way to access these files. I am working on creating this using Visual Studio 2013 and using visual C# windows forms. I have come across a snag however that I can't seem to find a workaround for. I am by no means fluent in C#, but I need to have a button on the windows form open a file without specifying what drive it comes from. I understand that I have to specify a path, but as these will be stored on the flash drives of myself and my coworkers I cannot foresee that the path will always begin with E:. Depending on what USB slot the drive is plugged into it could be N: or F: or the like. I have provided an example below:
Using what I currently know I am opening files using this line of code:
System.Diagnostics.Process.Start("C:/Users/Myname/Desktop/Asmodeus/Anti-Virus/Anti-Virus Installers/avast_free_antivirus_setup.exe");
Is there any way possible I can have the file open simply from
System.Diagnostics.Process.Start("Asmodeus/Anti-Virus/Anti-Virus Installers/avast_free_antivirus_setup.exe");
or something of that nature?
Thanks in advance.
There must have been some mis-communication when I asked my question previously. what I am looking to do is open an executable file via a button click on the windows form using a relative path. I am not able to specify the absolute path because the application will be run from a flash drive and therefore will change depending on what USB slot it is currently inserted into.
What I am hoping to accomplish is insert a line of code that will allow me to open an executable file that is located in the \bin\debug folder along with the application itself. I have a picture for clarification but apparently do not have enough reputation to post it. Thank you and sorry for the earlier confusion.
Usually you can just use Environment.GetFolderPath (MSDN) to give you what you need. It doesn't do absolutely everything, but if you need Desktop and the like, that is plenty.
Depending on the target version of .Net, the SpecialFolders exposed are not all there. It may turn out that you need more than they provide, but in your case it doesn't sound like it.
If there is more you need that is not covered in the default, check out this project. I'm sure there are others like it, but it does a little more than the default BCL version, using the API directly. It is at least something to read and learn (and translate from vb.. use an online translator, very quick). I haven't looked at it, but it seems like you are learning this c#/.net thingy, so it might be helpful
This article is about accessing Windows special folders.
These folders include your “Favorites”, “Cookies”, system libraries and the like.
Here is code, including a large number of constant definitions, plus documentation,
allowing access to and creation of these folders.

Accessing menustrip of other process

Im working on automating the behavious of a scientific measurement program in C#.
The program does have a .net API, but the developers of the program havent included all necessary commands eg. they havent included a simple "save as" command that i need for saving the measureing data.
Is there a way to operate the menu strip of the application and e.g. press the save as button.
I start the program from my own application by means of Process.Start();
One simple solution - copy destination path to the clipboard, then use e.g. SendKeys.SendWait() to send the following keys to your target application (with sufficient pauses between them) Ctrl+S "^s", Ctrl+V "^v", Enter "{ENTER}".
It's also a good idea to verify the folder exists, and the file does not, otherwise the saving will obviously fail, or overwrite confirmation will popup.
P.S. But before doing that, use e.g. .NET Reflector to ensure their .NET API doesn't have the save functionality: there're many cases where the SendKeys approach will fail.

Is there a way of capturing debug messages in C# and then outputting them to a separate window?

I have spent 3 days looking this up and I cannot find a solid answer. I want to capture debug messages and than output them to a list log. I am trying to do this in C#. Would love some help from the community to point me in the right direction.
Well, sure that's possible. Debug output is not limited to being displayed in the Output Window.
All you have to do is write a custom listener and add it to the Debug.Listeners collection. You can find the full documentation about adding trace listeners here on MSDN.
The only thing to watch out for is that the listeners are shared for debug and trace output, so if you add one, you'll receive both types of messages.
If that sounds like too much work and you just need a quick-and-dirty solution, you can download the free DebugView utility from Sysinternals. This neat little tool is a separate application that you run, and it listens to all debugging output from all of the programs installed on the machine. If you use this, you won't even have to change a single line of code in your application—all of the output send to Debug.Write will show up in the DebugView window.
Alternatively, if you're looking for something long-term that you could perhaps even ship with your application, I would encourage you to investigate adding a logging feature. There are lots of good open source libraries that provide this functionality, and it can be invaluable out in the field when your app is deployed to systems with unknown configurations.
As far as logging goes, Log4Net is solid and easy to use. You can add it to your project via NuGet (if you don't know what NuGet is, check it out: it's really nice). It allows you to log more or less wherever you want (console, file, mail, db ... )
Website contains good documentation and examples.

Write Resource file for .net project using the command line

I need to create a resource file for a .net project (by hand) and compile it using the ResGen.exe tool provided by the .NET framework. I can't find any documentation for this. I need to write the resource file by hand because I'm in a situation where I don't want to download/buy extra tools (like VS) to generate this resource file, and also I feel more productive through the command-line (helps me understand how things really work).
So I need to write a resource file by hand to store an ICON in the executable and use it from within my program. I would also like to use this icon to represent my executable in Windows Explorer.
Any references would be great!
Visual C# Express Edition will do what you want for free. If nothing else you can download that, create the resource file and then use that as a subject for your admirable curiosity about 'how it really works'. This may also save you some time in manual experimentation to get it right the first time around.
These 2 links in conjunction provide information on using that tool to create and embed an icon file, it seems specific to C#. Of course i'm guessing at your full intention, let me know if this points you in the proper direction.
http://www.xtremedotnettalk.com/showthread.php?t=75449
specifically there is a post which states;
I think you should first create a *.resources-File from the Icon with the tool named "Resgen.exe"...
resgen App.ico App.ico.resources
the next step would be compiling...
csc /t:winexe /out:Keygen.exe /res:App.ico.resources /r:Crypto.dll /win32icon:App.ico Keygen.cs AssemblyInfo.cs
I'm sure you were here already.
http://msdn.microsoft.com/en-us/library/ccec7sz1(VS.80).aspx
You should check this link:
http://msdn.microsoft.com/en-us/library/ekyft91f.aspx
It explains what formatter is used and gives some code samples to generate one from code. You could then write a small wrapper app that you can call from the command line. No downloads needed!

Categories