royatl
royatl

Reputation: 336

Using Excel as log reader. How to write to log file if Excel has it open?

I've come into a position where all the existing scheduled applications write their logs to .CSV files, so that people can open them in Excel. The problem comes when someone opens a log file and just leaves it open. The applications then can't write to their logs, and by rule, they die.

So the options are:

  1. somehow get the users to open the files in Excel in a non-exclusive manner;
  2. somehow be able to write to the log despite the users' rudeness;
  3. write a kinder, gentler log reader (nope, the company runs on Excel and .csv files and they won't change);
  4. ???????

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

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

Upvotes: 3

Views: 1020

Answers (2)

Phylogenesis
Phylogenesis

Reputation: 7880

If you set NTFS permissions on the folder containing the CSV files appropriately so that your users only have read access whilst the user your scheduled tasks are running as has full access, then Excel will open the files in Read Only mode.

This will prevent Excel from holding a lock on the files and allow your logging to continue unhindered.

Upvotes: 4

James Duffield
James Duffield

Reputation: 34

You could potentially make use of a logging framework like Serilog.

When you configure Serilog, you can define multiple outputs (Serilog calls them Sinks). So, for example, you could configure your applications to write to a Windows Event Log as well as a CSV file. Your applications could continue to log to the Event Log even if someone has a CSV file open.

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 simply need:

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

There are some other elements which need to be set up, but the documentation on their website is great and will get you started.

Serilog is very lightweight and quick to configure so it wouldn't take too much effort to update your applications to use it.

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

I'd highly recommend it as a logging framework.

(I am not involved in the project - just a big fan having used it multiple times in projects I've worked on).

Upvotes: 0

Related Questions