nxlogfile_dll_visualstudio/NxLogfile/NxLogfile.cs
Hoeglinger Eugen 63e6d18f3a Initialized
- Auslesen aller NX Logdateien aus einem definierten Verzeichnis
- Anzeigen der Aktuellen NX Logdatei
- Anzeigen der letzten Logdatei
- Anzeigen einer beliebigen Logdatei
- Liste aller Logdateien im Verzeichnis
2022-08-18 16:52:38 +02:00

246 lines
8.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Eugen.ESystem.IO;
namespace Eugen.NX
{
public class NxLogfile
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
if (startYear.Contains("-"))
{
startYear = startYear.Remove(startYear.IndexOf("-"));
}
else
{
startYear = startYear.Remove(startYear.IndexOf(" "));
}
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
string start = temp.Remove(temp.IndexOf(startYear) - 1);
string end = (temp.Remove(0, temp.IndexOf(" by")));
return String.Concat(start, " ", startYear, "-", currentYear, end);
}
}
#endregion
#region DLL-Info
/// <summary>
/// Contains the name of the dll file.
/// </summary>
public static string DllName { private set; get; }
/// <summary>
/// Contains the version of the dll file.
/// </summary>
public static string DllVersion { private set; get; }
/// <summary>
/// Contains the copyright notice.
/// </summary>
public static string Copyright { private set; get; }
/// <summary>
/// Contains the name-, version- and copyright-information of the dll file.
/// </summary>
public static string[] DllInfo { private set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
string[] dllInfo = new string[3];
dllInfo[0] = dllName;
dllInfo[1] = dllVersion;
dllInfo[2] = copyright;
DllInfo = dllInfo;
}
protected string ProgramName { private set; get; }
protected string ProgramVersion { private set; get; }
protected string ProgramCopyright { private set; get; }
#endregion
/// <summary>
/// Sets the DLL information
/// </summary>
static NxLogfile()
{
SetDllInfo();
}
/// <summary>
/// Reads out all NX log files stored in the specified directory
/// </summary>
/// <param name="nxLogFilePath">Directory in which the NX log files are stored</param>
public NxLogfile(string nxLogFilePath)
{
SetDllInfo();
LogFileExtension = "syslog";
NxLogFilePath = nxLogFilePath;
NxLogFiles = ReadNxLogfiles(NxLogFilePath, LogFileExtension);
CurrentNxLogFile = NxLogFiles[0];
LastNxLogFile = NxLogFiles[1];
}
private string NxLogFilePath { set; get; } //Verzeichnis in dem die NX-Logdateien gespeichert sind
/// <summary>
/// All NX log files stored in 'NxLogFilePath' (sorted)
/// </summary>
public string[] NxLogFiles { private set; get; } //Alle in 'NxLogFilePath' gespeicherten NX-Logdateien (sortiert)
/// <summary>
/// The current NX log file
/// </summary>
public string CurrentNxLogFile { private set; get; } //Die aktuelle NX-Logdatei
/// <summary>
/// The last NX log file
/// </summary>
public string LastNxLogFile { private set; get; } //Die letzte (aus der vorigen Sitzung) NX-Logdatei
private string LogFileExtension { set; get; } //Dateiendung einer NX-Logdatei
/// <summary>
/// Shows the specified log file
/// </summary>
/// <param name="fileName">The log file to display</param>
/// <exception cref="FileNotFoundException"></exception>
public void ShowNxLogFile(string fileName)
{
if (File.Exists(fileName))
{
ShowTextFile.Show(fileName);
}
else
{
throw new FileNotFoundException();
}
}
/// <summary>
/// Writes the log file to a string
/// </summary>
/// <param name="fileName">The log file to write</param>
/// <returns>A string</returns>
/// <exception cref="FileNotFoundException">If the log file was not found</exception>
public string GetNxLogFileAsString(string fileName)
{
try
{
return File.ReadAllText(fileName);
}
catch (Exception)
{
throw new FileNotFoundException();
}
}
private string[] ReadNxLogfiles(string path, string logFileExtension)
{
List<Logfile> logfiles = new List<Logfile>();
try
{
foreach (string file in Directory.GetFiles(path))
{
if (file.EndsWith(logFileExtension))
{
FileInfo f1 = new FileInfo(file);
logfiles.Add(new Logfile { FileName = file, CreationTime = f1.CreationTime.ToString() });
}
}
}
catch (Exception)
{
throw new FileNotFoundException();
}
var sortedLogfiles = logfiles.OrderBy(o => o.CreationTime).ToList();
sortedLogfiles.Reverse();
string[] logFileList = new string[sortedLogfiles.Count];
int x = 0;
foreach (var file in sortedLogfiles)
{
logFileList[x] = file.FileName;
x++;
}
return logFileList;
}
}
class Logfile
{
public string FileName { get; set; }
public string CreationTime { get; set; }
}
}