Reading Editor.log in the Editor (programmatically)?

It seems that I cannot read Editor.log programmatically, as it is locked:

var logFile = "C:/Users/Warwick/AppData/Local/Unity/Editor/Editor.log";
var all = System.IO.File.ReadAllText(logFile);

produces:

IOException: Sharing violation on path C:\Users\Warwick\AppData\Local\Unity\Editor\Editor.log
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
...

Is there any other way to access the log file?

Specifically I am trying to extract and process the Size information produced during build.

4 Answers

4

You need to open the file with file sharing…

FileMode fm = FileMode.Open;
string contents = “”;

        FileStream fs = new FileStream("C:/Users/Bob/AppData/Local/Unity/Editor/Editor.log", fm, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamReader sr = new StreamReader(fs);

        if (fs != null && sr != null)
        {
            contents = sr.ReadToEnd();
        }

I've heard you can use the WWW class to access files just about anywhere on the system if the URL starts with file:///

I've never tried it myself, but WWW is pretty easy to use.

I do have a vague memory of a post where using two or three slashes makes a difference.

This isn't the thread I'm thinking of, but it discusses using WWW to read files.

http://answers.unity3d.com/questions/159765/reading-a-local-file-from-webplayer-i-know-it-viol.html

I don't know if that will help you get around the fact that it's locked, though.

Is there no way to access asset size information without reading the file? I guess you're talking about the editor and it may not use the same optimized Assets.

I'll stop rambling now...

Sorry, I specifically mean programmatically. I'll improve my title.

You could probably write a PostprocessBuildPlayer script that copies the log file somewhere else.

You could copy it to your resources folder or your Assets folder and use it as a TextAsset.

Copying requires reading the file, which is the thing that doesn't work.

For some reason, using WWW to access the file rather than System.File.IO avoids the access violation. (thanks for the idea, @jahroy!)