3

I've written a C# console application that is being called by another program when that program completes.

My application is quite simple in that it accepts a number of command line arguments and then does some processing. I've added Serilog today in order to produce a log file instead of using the Console to display progress.

The application works, it does what it should and is feature complete. The following is how I create serilog:

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console()
                .WriteTo.File("logfile-.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

and I have Log.Debug/Warning/Information/Error where needed in my code.

If I run my code in debug or when I run it on the destination computer myself I can see the log appear in the console window and a logfile-.txt file is created with the output.

However the issue(s) I'm experiencing is when I build in release or debug and when the parent program calls it after it completes it's action my program runs, executes and does what it does but no log file is created or appended to.

As the program creates the log file when I or an administrator runs it I think it may be a permissions issue. I've tested with giving both the parent application and my application "run as administrator" permissions to no avail. Even giving EVERYONE full access to the folder where my program resides didn't result in a log file being created.

Would anyone know of something else I could look at or is there an option in serilog where I could look for it's own log when it is running in case there is an error that I'm not seeing?

Thank you,

Phil.

Edit #1:

I've been researching further and I've added the following to my Program:

Serilog.Debugging.SelfLog.Enable(new StreamWriter("serilog.txt"));

Compiled in both Release and Debug and if I run the program serilog.txt appears but if the parent application calls my program it does not.

Edit #2

Further researching led me to this question where the answer, for the OP (david w) was to add something to a .json file but I can't see a file in my project that matches that and I'm not 100% sure it applies to me.

I'm at a loss where to go next, any suggestions appreciated (and upvoted, when I can)

Phil Bevan
  • 56
  • 4
  • 1
    You could investigate using SysInternals Process Monitor to see when and where files are being writtten and/or refusing to be written? – Ruben Bartelink Mar 18 '22 at 08:51
  • @RubenBartelink Thank you for that suggestion, really thank you. I downloaded and ran, I'd forgotten HOW much it records but a little filtering later I found something weird: CreateFile C:\Users\Administrator\Desktop\log-20220318.txt Checking this I found the log, and there is also a Serilog Error file. They're not being written to the installation folder as I expected but to the Desktop of the user I'd RDP'd into. Looks like I need to find out why that's happening. Thank you again. – Phil Bevan Mar 18 '22 at 09:12
  • 1
    Cool you got it figured out - it should go without saying, but using the actual `administrator` account to do remote logins is a very bad idea - better to have a custom account with reduced privileges if at all possible. – Ruben Bartelink Mar 18 '22 at 20:55
  • Likely the current directory isn't what you think it is when the program is running. Since you specified a relative path in your configuration, that path is relative to the current directory, not relative to the applications installation directory. – mason Mar 19 '22 at 13:23

1 Answers1

0

The problem turned out to be my assumption of where the log was being written to was not correct. I've modified my code to write the log to the folder where my EXE is (and where it should have access):

.WriteTo.File(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), _logfilename), shared: true, rollingInterval: RollingInterval.Day)

Thanks to all for the help.

Phil Bevan
  • 56
  • 4