How do I write to a unity game's console (after build)? - c#

I am making a game server in unity and am running it in -batchmode. I have tried Console.WriteLine and Debug.Log but neither actually print anything to the console (I'm running in a .bat file). How should I write to the console? Is there a simple way to do it or how else should I display info about the server running?

Even without explicitly defining a different log location Unity by default always produces the following log outputs (See Log Files)
PackageManager Log
Linux
~/.config/unity3d/upm.log
macOS
~/Library/Logs/Unity/upm.log
Windows
C:\Users\username\AppData\Local\Unity\Editor\upm.log
Editor Log
Linux
~/.config/unity3d/Editor.log
macOS
~/Library/Logs/Unity/Editor.log
Windows
C:\Users\username\AppData\Local\Unity\Editor\Editor.log
Player Log
Linux
~/.config/unity3d/CompanyName/ProductName/Player.log
macOS
~/Library/Logs/Company Name/Product Name/Player.log
Windows
C:\Users\username\AppData\LocalLow\CompanyName\ProductName\Player.log
So after a build you would be interested in the last one, the player log. Usually it is created the moment you start the app but not filled with content until the app is fully closed.
The exception here is defining the -logFile - which directly prints the output to stdout/the terminal - see below.
If you want to redirect the log output to a different file or the command/stdout you can use (see Command Line Arguments)
-logFile <pathname>
Specify where Unity writes the Editor or Windows/Linux/OSX standalone log file. To output to the console, specify - for the path name. On Windows, specify - option to make the output go to stdout, which is not the console by default.
And use Debug.Log as usual.

Did some searching. It would seem that you can only log to a file if you're using -batchmode using the -logFile argument and Debug.Log.
I did manage to find this custom console example though:
https://garry.tv/unity-batchmode-console if you want to put in the extra effort to be able to write to console.
Similar question here might be useful for you
https://answers.unity.com/questions/884197/redirecting-build-output-to-console-for-automated.html?_ga=2.177817063.358316465.1597205884-742321341.1597081109

Related

How do you show console output from C# Windows app started by task manager?

I have a C# console application that sends constant output to the console via console.writeline. Works fine if I invoke it manually. Now I want it to be started by task scheduler. The app is started OK, but the output does not appear anywhere. The data is real time output so writing to a file is not an option.
How do I get it to show?
I cant write a comment (less than 50 rep), but you might also want look into this?
Where is Task Scheduler console output? (C# console app)
I know that this is for files again..
If your application does not have to be that real-time capable you can print the output into a text file and read that file from another program. If you do this remember to check if the file is available or right now read or written to by the other program

C# application says "No" when executed

I have developed a small-ish C# console application (TextMatcher.exe) on my local development machine and now need to deploy it to the live environment. It references another class library which I developed which has generic functions, which I intend to use and improve in future console applications.
Ultimately this specific application will be executed from within an SSIS package, but for now I'm just trying to run it from cmd.
I kid you not that this is the actual output from the program:
E:/TextMatcher>TextMatcher.exe
No
E:/TextMatcher>
The computer literally says "No" and gives no further information. I have not included, anywhere in the program, to output the word "No", on any failure or otherwise.
Of course, it runs fine locally. I made sure I included the dll of the utility class library too. I have read other questions (here, here) about how to deploy console apps correctly, and have followed the advice.
NB: This is also proving to be quite hard to Google because of the use of the word "No" being fundamental to the problem...
EDIT - It seems to be working now... I simply copied over the files again from my local machine to the remote machine... I am trying to get it to break again so that I can figure out what on Earth happened, and until I do, I will not accept an answer so that people could maybe shed some more light onto it. Either way it is quite baffling.
There's a chance that someone has modified the Image File Execution Options registry setting on the server to launch a debugger automatically.
In short, examine the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image file execution options and see if there's an entry that matches the name of your executable.
Check whether you have installed the necessary Framework components,
i.e. the suiting Dot net framework. Application with 4.0 and installed 3.5
can cause very strange behaviour.
Try writing a very simple window application and start it on the deployment machine
(this will give you probably more help what is missing).
Check whether the needed Dlls (that you developed, e.g. your class library) are reachable for the console application. And check whether the right version of your Dll is matched!
Check the plattform settings in your console application. Do they match with
the machine where you want to run your application? (win64 and win32 mismatch)
If all of those do not help, try inspecting your executable on the deployment machine
for example with depends.net, checkasm, or similar.
Does your production environment have AppLocker running? I don't know if its output can be customized to output "No" on a command line. Applocker comes to my mind because you can use it to restrict a system to run only signed executables. If your Textmatcher executable is unsigned, it may get shut down immediately. If you have the ability, I'd be curious to see if signing your binary changes the behavior.

calling C# exe from commandline using batch file

I want to create an exe file using C# which will send emails. I want to invoke this exe using batch file. Should I create a Web form application or just a class library ? Can an exe be called using batch file from command prompt?
Please suggest.
Typically, if you want to call a application from a command line, you'd create a Console Application.
This allows your command prompt to call the application and (optionally) accept input from the console (prompt) as well as write output to the console (via the Console class).
You can read the command line arguments directly from the Main routine in the Console Application, as well.
Write a console app that takes arguments on the command line. You can then call this from script (i.e. a batch file). The arguments that you pass could contain all the elements of your email - the body may be a little large and I am not sure of the limit (if there is one) for the length of string args, but as long as the body is reasonable in size you will be ok.
I prefer writing "tools" such like this as console apps (over Windows apps) because you have so much flexibility in terms of executing the app. You can invoke it directly by typing in the cmd.exe (the command line), you can write a script to call it (as you have suggested), you can write another .NET application that can call it, etc... A Windows app has just one method of invocation.
Of course. Create the application, and store the binary exe in some folder (i.e., C:\folder).
If your exe has the name "name.exe", in your batch file, write
cd C:\folder
name
This works for all types of applications, and you can access command-line data in both, but it's easiest in a console application.
You would write:
cd C:\Users\asif\Desktop\EmailSender\ConsoleApplication1\bin\Debug\
::Space here
ConsoleApplicati‌​on1
with the new line.

Running Windows Forms Application (C# .NET 4.0) from batch file with Command Line Arguments

I'm a Silverlight/ASP.NET developer trying to write my first Windows Forms application to run in the background on a server, populating our database. Eventually would like this to be a Windows service, but it's not required initially.
I need to create a batch file to execute 5 instances of this application, passing in the URL to 5 RESTful endpoints. So I published my app, which created a setup.exe. After installing it, I have an item that points to
C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\, with a .appref-ms file.
I'm not sure at this point what to do. Running:
"C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\StreamingApp.appref-ms" -"http://www.myURL.com" throws up a command window briefly, but the app doesn't run, data doesn't populate in DB.
What am I missing here?
since your application is in .exe format. And make your winform accepts command line arguments (check the main method) also make your Form ctor accepts params too. Then just launch it via cmd line just as you would other command, but here only to navigate to that dir where file exists.
In case of batch, use start command followed by program name and then arguments
It's hard to understand what is happening inside your application. You need to debug to understand what is going on there when it receives given parameters.
So I would suggest to debug an EXE. For this go to your EXE project properties, select DEBUG tab in CommandLineArguments insert your parameter string.
Run it in DEBUG and hopefully you will figure out a problem.
If after debugging it's not yet clear why it behaves in that way, come back to SO :)
Silvi if you plan to use your windows forms application from a batch file and you imagine the applicationm will behave differently in such mode than when opened witha double click, the usual approach is to parse the command line (arguments, also available in the main method as parameter) and to avoid loading the UI at all.
in fact if you have written your application properly the UI only managed the UI and does not contain the whole logic of database manipulation and data transformation.
what you could do is check inside the Main method if there are command line parameters and if you detect any of the special ones you have definded you really avoid to even call Application.Run(new Form1(...)); and start working in batch mode without user interface.
the same logic you want to use in batch mode or in UI mode can be wrapped in helper classes (often also called business managers or business logic... it depends), so that you do not have code duplication but simply UI or batch will call those classes nicely.

Get and set enviroment variables on mono and possible alternative

I got a little c# dll that implements a message queue service and on windows it runs perfectly.
To locate all the queues i use an enviroment variable stored in Machine during the installation.
System.Environment.SetEnvironmentVariable("PHMQ", install_dir , EnvironmentVariableTarget.Machine);
Now i just tryed to run it on Mono, and when the programs run it throw an exception.
The PHMQ service is not installed.
After 2 hours of search, i discovered that Mono doesn't really store this kind of variables it just send them to null.
(a simple text file was too simple ?)
So, the question is: How can i solve this problem ?
The first idea was : if is running on linux put it in a specific folder like /opt, but it's a dll, if do it in this way any program that want to use it must be executed like root
Any ideas ?
You could have your program append a line to /etc/environment to set the env var for the machine. Obviously to modify that file you'll need root, so it should be done during the installation process.
However, a known location would be better IMO. I can't see why you wouldn't be able to execute from /opt.

Categories

Resources