The DiningPhilosophers example demonstrates how to configure DCF to perform session based logging using an NLog adapter.
Classes
Class | Description | |
---|---|---|
Options |
Command line options class for parsing user options for DiningPhilosophers.
| |
Philosopher |
A Philosopher class that demonstrates per-session logging.
| |
Program |
This example demonstrates DCF session logging by running a number of threads that each log to their own session based logfile.
| |
WorkerThread |
WorkerThread is an abstract base class extended by the Philosopher class to implement thread behavior.
|
Structures
Structure | Description | |
---|---|---|
PhilosopherData |
The data items that every dining philosopher needs.
|
Remarks
Supported OS Platforms:
- Windows - .Net Framework 4.7.2 64-bit and 32-bit
- Windows - .Net Core 2.1 64-bit and 32-bit
- Linux - .Net Core 2.1 64-bit
Examples
DiningPhilosophers Sample Code
public static void Main(string[] args) { try { Options options; if (!Options.TryParse(args, out options)) { throw new ArgumentException("bad command line parameters"); } // For .NET Core applications, the build system apparently copies the App.config file to <AssemblyNameWithoutExtension>.dll.config instead of the // <AssemblyNameWithoutExtension>.exe.config. Also, the NLog 4.5.11 on .NET Core does not load configuration automatically from this .dll.config file. // Therefore, check below if we are building a .NET Core application or not. Even for non .NET Core applications, specify the path to the app config // file explicitly to make it clear which file is being used instead of relying on NLog default behavior. string executingAssemblyName = System.Reflection.Assembly.GetExecutingAssembly().Location; #if NETCOREAPP string nLogConfigFileName = Path.ChangeExtension(executingAssemblyName, ".dll.config"); #else string nLogConfigFileName = Path.ChangeExtension(executingAssemblyName, ".exe.config"); #endif LogManager.LogAdapter = new NLogLogAdapter(nLogConfigFileName); // Get the log directory path from the nlog configuration in our app config string logDir = NLogLogAdapter.GetNLogLogDirectoryPath("DiningPhilosophersRotatingLogger"); if (Directory.Exists(logDir)) { foreach (string file in Directory.GetFiles(logDir)) { File.Delete(file); } } Mutex[] chopSticks = new Mutex[options.PhilosophersCount]; // init the chopSticks for (int i = 0; i < options.PhilosophersCount; i++) chopSticks[i] = new Mutex(false); Philosopher[] philosophers = new Philosopher[options.PhilosophersCount]; // Create the Philosophers for (int i = 0; i < options.PhilosophersCount; i++) { PhilosopherData pd; pd.PhilosopherId = i + 1; pd.RightChopStick = chopSticks[(i - 1 + options.PhilosophersCount) % options.PhilosophersCount]; pd.LeftChopStick = chopSticks[i]; pd.AmountToEat = options.AmountToEat; pd.TotalFood = options.TotalFood; pd.MaxSleep = options.MaxSleep; philosophers[i] = new Philosopher(pd); philosophers[i].Start(); } Console.WriteLine("Output logs from this example are written to: {0}", logDir); Console.WriteLine("Waiting for the {0} philosophers threads to finish . . .", options.PhilosophersCount); for (int i = 0; i < options.PhilosophersCount; i++) { philosophers[i].Join(); } } catch (Exception e) { Console.WriteLine("Exception caught during execution: {0}", e); Logger.Error(e, "Exception caught during execution"); Environment.ExitCode = 1; } if (Debugger.IsAttached) { Console.Write("Press any key to continue . . . "); Console.ReadKey(); } }