Add SystemLogging
Change default logging behavior
This commit is contained in:
rykoma 2019-04-23 22:06:37 +09:00
Родитель 87f2af2384
Коммит 588f8fceaf
8 изменённых файлов: 89 добавлений и 42 удалений

Просмотреть файл

@ -133,6 +133,9 @@
<setting name="NewerInstallerPath" serializeAs="String"> <setting name="NewerInstallerPath" serializeAs="String">
<value /> <value />
</setting> </setting>
<setting name="SystemLogging" serializeAs="String">
<value>False</value>
</setting>
</Office365APIEditor.Properties.Settings> </Office365APIEditor.Properties.Settings>
</userSettings> </userSettings>
<applicationSettings> <applicationSettings>

Просмотреть файл

@ -23,7 +23,7 @@ namespace Office365APIEditor
private void OnFormClosed(object sender, FormClosedEventArgs e) private void OnFormClosed(object sender, FormClosedEventArgs e)
{ {
Util.WriteCustomLog("Office365APIEditor Closing Log", "Exit. Code 20"); Util.WriteSystemLog("Office365APIEditor Closing Log", "Exit. Code 20");
if (!string.IsNullOrEmpty(Properties.Settings.Default.NewerInstallerPath) && File.Exists(Properties.Settings.Default.NewerInstallerPath)) if (!string.IsNullOrEmpty(Properties.Settings.Default.NewerInstallerPath) && File.Exists(Properties.Settings.Default.NewerInstallerPath))
{ {
@ -76,6 +76,11 @@ namespace Office365APIEditor
} }
} }
// Turn off SystemLogging flag.
startupLog.AppendLine("Disable SystemLogging as default.");
Properties.Settings.Default.SystemLogging = false;
Properties.Settings.Default.Save();
// Check the command line switches. // Check the command line switches.
startupLog.AppendLine("CommandLine : " + Environment.CommandLine); startupLog.AppendLine("CommandLine : " + Environment.CommandLine);
string[] switches = Environment.GetCommandLineArgs(); string[] switches = Environment.GetCommandLineArgs();
@ -94,11 +99,11 @@ namespace Office365APIEditor
{ {
// Remove Run History file. // Remove Run History file.
startupLog.AppendLine("The history file will be delete."); startupLog.AppendLine("The history file will be delete.");
if (File.Exists(Path.Combine(Util.DefaultApplicationPath, "RunHistory.xml"))) if (File.Exists(Util.RunHistoryPath))
{ {
try try
{ {
File.Delete(Path.Combine(Util.DefaultApplicationPath, "RunHistory.xml")); File.Delete(Util.RunHistoryPath);
startupLog.AppendLine("The history file was deleted."); startupLog.AppendLine("The history file was deleted.");
} }
catch(Exception ex) catch(Exception ex)
@ -120,6 +125,13 @@ namespace Office365APIEditor
Properties.Settings.Default.Save(); Properties.Settings.Default.Save();
startupLog.AppendLine("CustomDefinedScopes setting was saved."); startupLog.AppendLine("CustomDefinedScopes setting was saved.");
} }
else if (command.ToLower() == ("/SystemLogging").ToLower())
{
// Turn on SystemLogging flag.
startupLog.AppendLine("Enable SystemLogging.");
Properties.Settings.Default.SystemLogging = true;
Properties.Settings.Default.Save();
}
} }
// Set default log folder path. // Set default log folder path.
@ -134,7 +146,7 @@ namespace Office365APIEditor
} }
// Write startup log. // Write startup log.
Util.WriteCustomLog("Office365APIEditor startup log", startupLog.ToString()); Util.WriteSystemLog("Office365APIEditor startup log", startupLog.ToString());
// Create the MyApplicationContext, that derives from ApplicationContext, // Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit. // that manages when the application should exit.
@ -182,7 +194,7 @@ namespace Office365APIEditor
} }
finally finally
{ {
Util.WriteCustomLog("Office365APIEditor Closing Log", "Exit. Code 10"); Util.WriteSystemLog("Office365APIEditor Closing Log", "Exit. Code 10");
} }
} }

Просмотреть файл

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.10.6.0")] [assembly: AssemblyVersion("0.10.6.1")]
[assembly: AssemblyFileVersion("0.10.6.0")] [assembly: AssemblyFileVersion("0.10.6.1")]

12
Office365APIEditor/Properties/Settings.Designer.cs сгенерированный
Просмотреть файл

@ -542,5 +542,17 @@ namespace Office365APIEditor.Properties {
this["NewerInstallerPath"] = value; this["NewerInstallerPath"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool SystemLogging {
get {
return ((bool)(this["SystemLogging"]));
}
set {
this["SystemLogging"] = value;
}
}
} }
} }

Просмотреть файл

@ -205,5 +205,8 @@
<Setting Name="NewerInstallerPath" Type="System.String" Scope="User"> <Setting Name="NewerInstallerPath" Type="System.String" Scope="User">
<Value Profile="(Default)" /> <Value Profile="(Default)" />
</Setting> </Setting>
<Setting Name="SystemLogging" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

Просмотреть файл

@ -105,7 +105,7 @@ namespace Office365APIEditor
Enabled = true; Enabled = true;
} }
Util.WriteCustomLog("AcquireViewerTokenForm", stringBuilder.ToString()); Util.WriteSystemLog("AcquireViewerTokenForm", stringBuilder.ToString());
} }
} }
} }

Просмотреть файл

@ -58,7 +58,7 @@ namespace Office365APIEditor
// Load Run History // Load Run History
try try
{ {
string runHistoryFilePath = Path.Combine(Util.DefaultApplicationPath, "RunHistory.xml"); string runHistoryFilePath = Util.RunHistoryPath;
if (File.Exists(runHistoryFilePath)) if (File.Exists(runHistoryFilePath))
{ {
using (FileStream stream = new FileStream(runHistoryFilePath, FileMode.Open)) using (FileStream stream = new FileStream(runHistoryFilePath, FileMode.Open))
@ -610,10 +610,7 @@ namespace Office365APIEditor
if (ex.Response == null) if (ex.Response == null)
{ {
// Logging // Logging
if (checkBox_Logging.Checked) WriteResponseErrorLog(ex.Message);
{
Util.WriteCustomLog("Response", ex.Message);
}
DisplayResponse("Error", null, ex.Message); DisplayResponse("Error", null, ex.Message);
@ -646,10 +643,7 @@ namespace Office365APIEditor
catch (Exception ex) catch (Exception ex)
{ {
// Logging // Logging
if (checkBox_Logging.Checked) WriteResponseErrorLog(ex.Message);
{
Util.WriteCustomLog("Response", ex.Message);
}
DisplayResponse("Error", null, ex.Message); DisplayResponse("Error", null, ex.Message);
@ -807,10 +801,7 @@ namespace Office365APIEditor
if (ex.Response == null) if (ex.Response == null)
{ {
// Logging // Logging
if (checkBox_Logging.Checked) WriteResponseErrorLog(ex.Message);
{
Util.WriteCustomLog("Response", ex.Message);
}
DisplayResponse("Error", null, ex.Message); DisplayResponse("Error", null, ex.Message);
} }
@ -837,10 +828,7 @@ namespace Office365APIEditor
catch (Exception ex) catch (Exception ex)
{ {
// Logging // Logging
if (checkBox_Logging.Checked) WriteResponseErrorLog(ex.Message);
{
Util.WriteCustomLog("Response", ex.Message);
}
DisplayResponse("Error", null, ex.Message); DisplayResponse("Error", null, ex.Message);
} }
@ -1063,6 +1051,18 @@ namespace Office365APIEditor
Util.WriteLog(sb); Util.WriteLog(sb);
} }
private void WriteResponseErrorLog(string ErrorMessage)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Response");
sb.AppendLine(ErrorMessage);
if (checkBox_Logging.Checked)
{
Util.WriteLog(sb);
}
}
private void AddRunHistory(WebRequest Request, string RequestHeader, string RequestBody, HttpWebResponse Response, string JsonResponse) private void AddRunHistory(WebRequest Request, string RequestHeader, string RequestBody, HttpWebResponse Response, string JsonResponse)
{ {
@ -1117,7 +1117,7 @@ namespace Office365APIEditor
try try
{ {
FileStream stream = new FileStream(Path.Combine(Util.DefaultApplicationPath, "RunHistory.xml"), FileMode.Create); FileStream stream = new FileStream(Util.RunHistoryPath, FileMode.Create);
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{ {

Просмотреть файл

@ -23,23 +23,29 @@ namespace Office365APIEditor
{ {
get get
{ {
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) string result = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Office365APIEditor");
{
// ClickOnce scenario
string result = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Office365APIEditor"); if (!Directory.Exists(result))
if (!Directory.Exists(result))
{
Directory.CreateDirectory(result);
}
return result;
}
else
{ {
return System.Windows.Forms.Application.StartupPath; Directory.CreateDirectory(result);
} }
return result;
}
}
public static string RunHistoryPath
{
get
{
string workingFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Office365APIEditor");
if (!Directory.Exists(workingFolder))
{
Directory.CreateDirectory(workingFolder);
}
return Path.Combine(workingFolder, "RunHistory.xml");
} }
} }
@ -149,18 +155,29 @@ namespace Office365APIEditor
}; };
} }
public static bool WriteCustomLog(string Title, string Message) public static bool WriteSystemLog(string Title, string Message)
{ {
// Write log if SystemLogging flag is true.
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendLine(Title); sb.AppendLine(Title);
sb.AppendLine("DateTime : " + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")); sb.AppendLine("DateTime : " + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
sb.AppendLine(Message); sb.AppendLine(Message);
return WriteLog(sb); if (Properties.Settings.Default.SystemLogging)
{
return WriteLog(sb);
}
else
{
return true;
}
} }
public static bool WriteLog(StringBuilder Message) public static bool WriteLog(StringBuilder Message)
{ {
// Write log.
Message.AppendLine(""); Message.AppendLine("");
string logFilePath = ""; string logFilePath = "";