Using Excel as a reader. How to write log file if Excel is open?

I ended up in a position where all existing scheduled applications write their logs to .CSV files so people can open them in Excel. The problem occurs when someone opens the log file and just leaves it open. Then applications cannot write their logs, and by rule they die.

So the options are:

  • somehow force users to open files in Excel in a non-exclusive way;
  • can somehow be written to the log, despite the rudeness of the users;
  • write a kind, gentle journal reader (no, the company works in Excel and CSV files and they won't change);
  • ???????

Or am I just missing a totally brainless way to deal with this?

Most applications are currently VBScript, but I have already managed to convert many of them to C # console applications, so answers in both styles are welcome.

+3


source to share


2 answers


If you set NTFS permissions on the folder containing the CSV files appropriately so that your users have read-only access while the user of your scheduled tasks is working with full control, Excel will open the files in read-only mode.



This will prevent blocking of Excel files and allow your protocol to continue running smoothly.

+3


source


You can potentially use a logging framework like Serilog.

When configuring Serilog, you can define multiple outputs (Serilog calls them Sinks). For example, you can configure applications to write to the Windows event log as well as a CSV file. Your apps can continue to be logged to the event log even if someone has a CSV file.

An example configuration could be:

var log = new LoggerConfiguration()
   .WriteTo.File("log.csv")
   .WriteTo.EventLog("My App")
   .CreateLogger();

      

Then, to write to the log, you just need to:

log.Information("This is a sample log entry");

      



There are some other elements to tweak, but the documentation on their website is great and you get started.

Serilog is very lightweight and fast to set up, so it doesn't take too much effort to update apps to use it.

Here is the Serilog homepage: https://github.com/serilog/serilog/wiki

I highly recommend it as a registration framework.

(I am not involved in the project - just a big fan who has used it several times on projects I have worked on).

0


source







All Articles