diff --git a/.vs/LogWriter.git/FileContentIndex/1225c7fe-1a5a-4feb-906d-80645975aaa4.vsidx b/.vs/LogWriter.git/FileContentIndex/1225c7fe-1a5a-4feb-906d-80645975aaa4.vsidx new file mode 100644 index 0000000..291959d Binary files /dev/null and b/.vs/LogWriter.git/FileContentIndex/1225c7fe-1a5a-4feb-906d-80645975aaa4.vsidx differ diff --git a/.vs/LogWriter.git/FileContentIndex/28c9315f-4687-48e2-b323-98ceb1931b11.vsidx b/.vs/LogWriter.git/FileContentIndex/28c9315f-4687-48e2-b323-98ceb1931b11.vsidx deleted file mode 100644 index 608dfc7..0000000 Binary files a/.vs/LogWriter.git/FileContentIndex/28c9315f-4687-48e2-b323-98ceb1931b11.vsidx and /dev/null differ diff --git a/.vs/LogWriter.git/FileContentIndex/80707f09-c282-44b0-a51f-c709bc0bc1d0.vsidx b/.vs/LogWriter.git/FileContentIndex/80707f09-c282-44b0-a51f-c709bc0bc1d0.vsidx new file mode 100644 index 0000000..046b3c9 Binary files /dev/null and b/.vs/LogWriter.git/FileContentIndex/80707f09-c282-44b0-a51f-c709bc0bc1d0.vsidx differ diff --git a/.vs/LogWriter.git/v17/.suo b/.vs/LogWriter.git/v17/.suo index 9c68fe7..7f3ecd0 100644 Binary files a/.vs/LogWriter.git/v17/.suo and b/.vs/LogWriter.git/v17/.suo differ diff --git a/.vs/LogWriter.git/v17/DocumentLayout.json b/.vs/LogWriter.git/v17/DocumentLayout.json index d1b656e..8378e5c 100644 --- a/.vs/LogWriter.git/v17/DocumentLayout.json +++ b/.vs/LogWriter.git/v17/DocumentLayout.json @@ -3,11 +3,15 @@ "WorkspaceRootPath": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\", "Documents": [ { - "AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "RelativeMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|solutionrelative:logwriter\\logwriter.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" }, { - "AbsoluteMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "AbsoluteMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\logwriter\\formattinglongstrings.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{E95B9315-E0B9-4DB0-BA06-CAEA7269E922}|LogWriter\\LogWriter.csproj|solutionrelative:logwriter\\formattinglongstrings.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|c:\\cad\\git-workrepository\\visualstudio\\_dll\\logwriter.git\\test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "RelativeMoniker": "D:0:0:{9414648E-B3A3-4279-B2E1-F54E35326E93}|Test_LogWriter\\Test_LogWriter.csproj|solutionrelative:test_logwriter\\form1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" }, { @@ -22,7 +26,7 @@ "DocumentGroups": [ { "DockedWidth": 200, - "SelectedChildIndex": 6, + "SelectedChildIndex": 7, "Children": [ { "$type": "Bookmark", @@ -43,27 +47,39 @@ { "$type": "Document", "DocumentIndex": 1, + "Title": "FormattingLongStrings.cs", + "DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\FormattingLongStrings.cs", + "RelativeDocumentMoniker": "LogWriter\\FormattingLongStrings.cs", + "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\FormattingLongStrings.cs", + "RelativeToolTip": "LogWriter\\FormattingLongStrings.cs", + "ViewState": "AQIAAAMAAAAAAAAAAAAAAOQAAABFAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-06-14T12:42:52.903Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 2, "Title": "Form1.cs", "DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "RelativeToolTip": "Test_LogWriter\\Form1.cs", - "ViewState": "AQIAAHUAAAAAAAAAAAAowJcAAAAMAAAA", + "ViewState": "AQIAAIEAAAAAAAAAAAAAAJwAAABzAAAA", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "WhenOpened": "2024-06-11T12:34:35.414Z", "EditorCaption": "" }, { "$type": "Document", - "DocumentIndex": 2, + "DocumentIndex": 3, "Title": "Form1.cs [Entwurf]", "DocumentMoniker": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs", "RelativeDocumentMoniker": "Test_LogWriter\\Form1.cs", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\Test_LogWriter\\Form1.cs [Entwurf]", "RelativeToolTip": "Test_LogWriter\\Form1.cs [Entwurf]", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", - "WhenOpened": "2024-06-11T12:34:35.436Z", - "EditorCaption": " [Entwurf]" + "WhenOpened": "2024-06-11T12:34:35.436Z" }, { "$type": "Document", @@ -73,7 +89,7 @@ "RelativeDocumentMoniker": "LogWriter\\LogWriter.cs", "ToolTip": "C:\\CAD\\Git-WorkRepository\\VisualStudio\\_DLL\\LogWriter.git\\LogWriter\\LogWriter.cs", "RelativeToolTip": "LogWriter\\LogWriter.cs", - "ViewState": "AQIAAAAAAAAAAAAAAAAAAI8AAAAIAAAA", + "ViewState": "AQIAABUCAAAAAAAAAAAAADECAAB0AAAA", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "WhenOpened": "2024-06-11T12:34:36.369Z", "EditorCaption": "" diff --git a/LogWriter/FormattingLongStrings.cs b/LogWriter/FormattingLongStrings.cs new file mode 100644 index 0000000..bbcdc76 --- /dev/null +++ b/LogWriter/FormattingLongStrings.cs @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Eugen.ESystem.Text +{ + public class FormattingLongStrings + { + #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 + /// + /// Contains the name of the dll file. + /// + public static string DllName { private set; get; } + + /// + /// Contains the version of the dll file. + /// + public static string DllVersion { private set; get; } + + /// + /// Contains the copyright notice. + /// + public static string Copyright { private set; get; } + + /// + /// Contains the name-, version- and copyright-information of the dll file. + /// + 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 + + static FormattingLongStrings() + { + SetDllInfo(); + } + + //public FormattingLongStrings() + //{ + // SetDllInfo(); + //} + + /// + /// Defines the length type + /// + public enum LengthType + { + LineLength, + IndentLength + } + + /// + /// Splits a longer text into sections with a defined maximum length for each section + /// + /// Text to be edited + /// Length of the first line in characters + /// Length of the following line or the indentation in characters + /// Defines whether the line length or the indentation is measured + /// Returns a string array with individual lines + public static string[] FormattingString(string text, int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType) + { + // If the text line is longer than the desired first line length, then split it, otherwise it is already ok + if (text.Length > lengthFirstLine) + { + lengthSubsequentLines = LengthSubsequentLines(lengthFirstLine, lengthSubsequentLines, lengthType); // The text length of the other lines is calculated + int indentLength = lengthFirstLine - lengthSubsequentLines; ; + + List lines = new List(); + + lines.Add(CreateLine(text, lengthFirstLine)); // Create the first line in the length of the first line + + text = ShortenStringByAlreadyUsed(text, lines[0]); // The text already used is removed from the text + + text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it + + string tempText = ""; + + // If the text line is longer than the desired line length, then split it + while (text.Length > lengthSubsequentLines) + { + tempText = CreateLine(text, lengthSubsequentLines); + + // Generate the other lines with the corresponding length + if (lengthType == LengthType.LineLength) + { + lines.Add(tempText); // If the line length of the additional lines + } + else + { + // Line length is the length of the first line minus the length of the indentation + // Spaces in the number of indents are placed in front of the text + lines.Add(tempText.PadLeft(indentLength + tempText.Length, ' ')); + } + + text = ShortenStringByAlreadyUsed(text, tempText); // The text already used is removed from the text + + text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it + } + + // If the text still contains characters, then edit + if (text.Length > 0) + { + if (lengthType == LengthType.LineLength) + { + lines.Add(text); // If the line type is 'Line length' then add + } + else + { + lines.Add(text.PadLeft(indentLength + text.Length, ' ')); // Otherwise, precede with a space and then add + } + } + + return lines.ToArray(); + } + else + { + string[] value = {text}; // If the text is shorter than the line length, then use it + return value; + } + } + + private static int LengthSubsequentLines(int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType) + { + // Returns the length of the subsequent lines, depending on the line type + if (lengthType == LengthType.LineLength) + { + return lengthSubsequentLines; // Line length + } + else + { + return lengthFirstLine - lengthSubsequentLines; // Line length with indentation + } + } + + private static string CreateLine(string text, int lineLength) + { + // Returns a text section of the specified length + string tempLine = text.Remove(lineLength); + string tempRemainingLine = text.Remove(0, lineLength); + + // If the next part begins with a space, the line ends with a whole word + if (tempRemainingLine.StartsWith(" ")) + { + return tempLine; // Then use the line as it is + } + else + { + if (tempLine.Contains(" ")) + { + return tempLine.Remove(tempLine.LastIndexOf(" ")); // Otherwise shorten the line up to the last space + } + else + { + return tempLine; // Then use the line as it is + } + } + } + + private static string ShortenStringByAlreadyUsed(string text, string usedCharacters) + { + // Remove the piece of text used from the entire text + return text.Replace(usedCharacters, ""); + } + + private static string RemoveLeadingSpaces(string text) + { + // If the text line begins with a space, remove the space + while (text.StartsWith(" ")) + { + text = text.Remove(0, 1); + } + + return text; + } + } +} diff --git a/LogWriter/LogWriter.cs b/LogWriter/LogWriter.cs index 8da57cf..b0e4e0d 100644 --- a/LogWriter/LogWriter.cs +++ b/LogWriter/LogWriter.cs @@ -1,8 +1,10 @@ -using System; +using Eugen.ESystem.Text; +using System; using System.Diagnostics; using System.IO; using System.Reflection; using static Eugen.ESystem.IO.LogWriter; +using static System.Net.Mime.MediaTypeNames; namespace Eugen.ESystem.IO { @@ -197,6 +199,10 @@ namespace Eugen.ESystem.IO } } + private int lineLength = 80; + private int indentLengthWithoutLogLevel = 15; + private int indentLengthWithLogLevel = 23; + private void SetDate() { DateTime dateTime = DateTime.Now; @@ -523,6 +529,17 @@ namespace Eugen.ESystem.IO private bool WriteLine(string logLevel, DateTime time, int ms, string message) { + int indentLength = 0; + + if (logLevel == "") + { + indentLength = indentLengthWithoutLogLevel; + } + else + { + indentLength = indentLengthWithLogLevel; + } + try { StreamWriter writer; @@ -540,8 +557,18 @@ namespace Eugen.ESystem.IO } // Zeile schreiben - writer.WriteLine(message); - + if (message.Length > lineLength) + { + foreach (string line in FormattingLongStrings.FormattingString(message, lineLength, indentLength, FormattingLongStrings.LengthType.IndentLength)) + { + writer.WriteLine(line); + } + } + else + { + writer.WriteLine(message); + } + // Logdatei schließen writer.Close(); diff --git a/LogWriter/LogWriter.csproj b/LogWriter/LogWriter.csproj index a2d57aa..fa684ac 100644 --- a/LogWriter/LogWriter.csproj +++ b/LogWriter/LogWriter.csproj @@ -40,6 +40,7 @@ + diff --git a/LogWriter/bin/Debug/helogwri.dll b/LogWriter/bin/Debug/helogwri.dll index 68d73e9..5dd1272 100644 Binary files a/LogWriter/bin/Debug/helogwri.dll and b/LogWriter/bin/Debug/helogwri.dll differ diff --git a/LogWriter/bin/Debug/helogwri.pdb b/LogWriter/bin/Debug/helogwri.pdb index 7160473..29e866b 100644 Binary files a/LogWriter/bin/Debug/helogwri.pdb and b/LogWriter/bin/Debug/helogwri.pdb differ diff --git a/LogWriter/obj/Debug/LogWriter.csproj.CoreCompileInputs.cache b/LogWriter/obj/Debug/LogWriter.csproj.CoreCompileInputs.cache index 2523a93..17dbb67 100644 --- a/LogWriter/obj/Debug/LogWriter.csproj.CoreCompileInputs.cache +++ b/LogWriter/obj/Debug/LogWriter.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -e059c56f5aea4ad7e9b0a0f639bec4193ddedaf32239362c2db6cc7c7ffcade6 +4c670e3294eecc743f9ea4d797951847f22f5cb8e82909513a5e79cc343002b8 diff --git a/LogWriter/obj/Debug/helogwri.dll b/LogWriter/obj/Debug/helogwri.dll index 68d73e9..5dd1272 100644 Binary files a/LogWriter/obj/Debug/helogwri.dll and b/LogWriter/obj/Debug/helogwri.dll differ diff --git a/LogWriter/obj/Debug/helogwri.pdb b/LogWriter/obj/Debug/helogwri.pdb index 7160473..29e866b 100644 Binary files a/LogWriter/obj/Debug/helogwri.pdb and b/LogWriter/obj/Debug/helogwri.pdb differ diff --git a/Test_LogWriter/Form1.cs b/Test_LogWriter/Form1.cs index 73f09b1..e7e2cd6 100644 --- a/Test_LogWriter/Form1.cs +++ b/Test_LogWriter/Form1.cs @@ -3,6 +3,7 @@ using System.IO; using System.Reflection; using System.Windows.Forms; using Eugen.ESystem.IO; +using Eugen.ESystem.Text; namespace Test_LogWriter { @@ -145,7 +146,7 @@ namespace Test_LogWriter logWriter3.Delete(); logWriter3.WriteHeader(LogWriter.Header.Default); - logWriter3.WriteMessage("Das ist Test 3"); + logWriter3.WriteMessage("Das ist Test 3. Dabei handelt es sich um eine sehr lange Log-Message. Die ist höchstwahrscheinlich länger als die 80 Zeichen, die in eine Zeile geschrieben werden. Darum soll sie auch umgebrochen werden."); labelResult.Text = String.Concat(labelResult.Text, "\nDie Log-Datei C:\\TMP\\my_logfile3.log wurde geschrieben"); //Variante 4 - Logdateiname wird angegeben und ein Anwender definierter Header wird geschrieben @@ -153,7 +154,7 @@ namespace Test_LogWriter logWriter4.Delete(); logWriter4.WriteHeader(LogWriter.Header.Userdefined); - logWriter4.WriteMessage("Das ist Test 4", LogWriter.LogLevel.Information, LogWriter.Time.Write); + logWriter4.WriteMessage("Das ist Test 4. Was passiert wenn lange kein Leerzeichen kommt. z.B.1234567890123456789012345678901234567890123456789012345678901234567890", LogWriter.LogLevel.Information, LogWriter.Time.Write); logWriter4.WriteMessage("Eine Zeile ohne Log Level", LogWriter.LogLevel.NoLevel, LogWriter.Time.Write); logWriter4.WriteMessage("Und noch eine Zeile mit Log Level", LogWriter.LogLevel.Warning, LogWriter.Time.Write); logWriter4.WriteSeparationLine(); diff --git a/Test_LogWriter/bin/Debug/Test_LogWriter.exe b/Test_LogWriter/bin/Debug/Test_LogWriter.exe index a99b740..a21be45 100644 Binary files a/Test_LogWriter/bin/Debug/Test_LogWriter.exe and b/Test_LogWriter/bin/Debug/Test_LogWriter.exe differ diff --git a/Test_LogWriter/bin/Debug/Test_LogWriter.pdb b/Test_LogWriter/bin/Debug/Test_LogWriter.pdb index 9d337d8..afbf883 100644 Binary files a/Test_LogWriter/bin/Debug/Test_LogWriter.pdb and b/Test_LogWriter/bin/Debug/Test_LogWriter.pdb differ diff --git a/Test_LogWriter/bin/Debug/helogwri.dll b/Test_LogWriter/bin/Debug/helogwri.dll index 68d73e9..5dd1272 100644 Binary files a/Test_LogWriter/bin/Debug/helogwri.dll and b/Test_LogWriter/bin/Debug/helogwri.dll differ diff --git a/Test_LogWriter/bin/Debug/helogwri.pdb b/Test_LogWriter/bin/Debug/helogwri.pdb index 7160473..29e866b 100644 Binary files a/Test_LogWriter/bin/Debug/helogwri.pdb and b/Test_LogWriter/bin/Debug/helogwri.pdb differ diff --git a/Test_LogWriter/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/Test_LogWriter/obj/Debug/DesignTimeResolveAssemblyReferences.cache index ef40e6b..5a13ae1 100644 Binary files a/Test_LogWriter/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/Test_LogWriter/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/Test_LogWriter/obj/Debug/Test_LogWriter.csproj.AssemblyReference.cache b/Test_LogWriter/obj/Debug/Test_LogWriter.csproj.AssemblyReference.cache index 3d0cb9d..c50065e 100644 Binary files a/Test_LogWriter/obj/Debug/Test_LogWriter.csproj.AssemblyReference.cache and b/Test_LogWriter/obj/Debug/Test_LogWriter.csproj.AssemblyReference.cache differ diff --git a/Test_LogWriter/obj/Debug/Test_LogWriter.exe b/Test_LogWriter/obj/Debug/Test_LogWriter.exe index a99b740..a21be45 100644 Binary files a/Test_LogWriter/obj/Debug/Test_LogWriter.exe and b/Test_LogWriter/obj/Debug/Test_LogWriter.exe differ diff --git a/Test_LogWriter/obj/Debug/Test_LogWriter.pdb b/Test_LogWriter/obj/Debug/Test_LogWriter.pdb index 9d337d8..afbf883 100644 Binary files a/Test_LogWriter/obj/Debug/Test_LogWriter.pdb and b/Test_LogWriter/obj/Debug/Test_LogWriter.pdb differ