-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
291 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MetroTrilithon.Linq; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using SylphyHorn.Interop; | ||
using SylphyHorn.Properties; | ||
|
||
namespace SylphyHorn | ||
{ | ||
partial class Application | ||
{ | ||
public static CommandLineArgs Args { get; private set; } | ||
|
||
public static TelemetryClient TelemetryClient { get; } | ||
|
||
public new static Application Current => (Application)System.Windows.Application.Current; | ||
|
||
static Application() | ||
{ | ||
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; | ||
|
||
TelemetryClient = new TelemetryClient(); | ||
TelemetryClient.Context.Session.Id = Guid.NewGuid().ToString(); | ||
TelemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString(); | ||
TelemetryClient.Context.Component.Version = ProductInfo.VersionString; | ||
#if DEBUG | ||
TelemetryClient.Context.User.Id = Environment.UserName; | ||
#endif | ||
SetInstrumentationKey(); | ||
} | ||
|
||
static partial void SetInstrumentationKey(); | ||
|
||
private static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args) | ||
{ | ||
if ((DateTime.Now - Process.GetCurrentProcess().StartTime).TotalMinutes >= 3) | ||
{ | ||
// 3 分以上生きてたら安定稼働と見做して再起動させる | ||
Restart(); | ||
} | ||
else | ||
{ | ||
ReportException("AppDomain", sender, args.ExceptionObject as Exception); | ||
} | ||
} | ||
|
||
private static void Restart() | ||
{ | ||
if (Args != null) | ||
{ | ||
var restartCount = Args.Restarted ?? 0; | ||
|
||
Process.Start( | ||
Environment.GetCommandLineArgs()[0], | ||
Args.Options | ||
.Where(x => x.Key != Args.GetKey(nameof(CommandLineArgs.Restarted))) | ||
.Concat(EnumerableEx.Return(Args.CreateOption(nameof(CommandLineArgs.Restarted), (restartCount + 1).ToString()))) | ||
.Select(x => x.ToString()) | ||
.JoinString(" ")); | ||
} | ||
} | ||
|
||
|
||
private static void ReportException(string caller, object sender, Exception exception) | ||
{ | ||
try | ||
{ | ||
var now = DateTimeOffset.Now; | ||
var path = Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
ProductInfo.Company, | ||
ProductInfo.Product, | ||
"ErrorReports", | ||
$"ErrorReport-{now:yyyyMMdd-HHmmss}-{now.Millisecond:000}.log"); | ||
|
||
var message = $@"*** Error Report ({caller}) *** | ||
{ProductInfo.Product} ver.{ProductInfo.VersionString} | ||
{now} | ||
{new SystemEnvironment()} | ||
Sender: {(sender is Type t ? t : sender?.GetType())?.FullName} | ||
Exception: {exception?.GetType().FullName} | ||
{exception} | ||
"; | ||
// ReSharper disable once AssignNullToNotNullAttribute | ||
Directory.CreateDirectory(Path.GetDirectoryName(path)); | ||
File.AppendAllText(path, message); | ||
|
||
TelemetryClient.TrackException(exception); | ||
TelemetryClient.TrackTrace(message, SeverityLevel.Critical); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine(ex); | ||
} | ||
|
||
Current.Shutdown(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
| ||
namespace SylphyHorn | ||
{ | ||
partial class Application | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using Microsoft.Win32; | ||
|
||
// This example displays output like the following: | ||
// .NET Framework Version: 4.6.1 | ||
// | ||
// see also: https://docs.microsoft.com/ja-jp/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed | ||
|
||
namespace SylphyHorn.Interop | ||
{ | ||
public class DotnetVersion | ||
{ | ||
public static string GetVersion() | ||
{ | ||
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"; | ||
var version = ""; | ||
|
||
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)) | ||
{ | ||
if (ndpKey?.GetValue("Release") is int value) | ||
{ | ||
version = GetVersionCore(value); | ||
} | ||
} | ||
|
||
if (string.IsNullOrEmpty(version)) | ||
{ | ||
version = Environment.Version.ToString(); | ||
} | ||
|
||
return $".NET Framework Version: {version}"; | ||
} | ||
|
||
private static string GetVersionCore(int releaseKey) | ||
{ | ||
if (releaseKey >= 461808) return "4.7.2 or later"; | ||
if (releaseKey >= 461308) return "4.7.1"; | ||
if (releaseKey >= 460798) return "4.7"; | ||
if (releaseKey >= 394802) return "4.6.2"; | ||
if (releaseKey >= 394254) return "4.6.1"; | ||
if (releaseKey >= 393295) return "4.6"; | ||
if (releaseKey >= 379893) return "4.5.2"; | ||
if (releaseKey >= 378675) return "4.5.1"; | ||
if (releaseKey >= 378389) return "4.5"; | ||
|
||
return ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management; | ||
|
||
namespace SylphyHorn.Interop | ||
{ | ||
public class SystemEnvironment | ||
{ | ||
public string OS { get; } | ||
public string OSVersion { get; } | ||
public string Architecture { get; } | ||
|
||
public string CPU { get; } | ||
public string TotalPhysicalMemorySize { get; } | ||
public string FreePhysicalMemorySize { get; } | ||
|
||
public string DotNetVersion { get; } | ||
|
||
public string ErrorMessage { get; } | ||
|
||
public SystemEnvironment() | ||
{ | ||
try | ||
{ | ||
using (var managementClass = new ManagementClass("Win32_OperatingSystem")) | ||
using (var managementObject = managementClass.GetInstances().OfType<ManagementObject>().FirstOrDefault()) | ||
{ | ||
if (managementObject == null) return; | ||
|
||
this.OS = managementObject["Caption"].ToString(); | ||
this.OSVersion = managementObject["Version"].ToString(); | ||
this.Architecture = managementObject["OSArchitecture"].ToString(); | ||
|
||
this.TotalPhysicalMemorySize = $"{managementObject["TotalVisibleMemorySize"]:N0} KB"; | ||
this.FreePhysicalMemorySize = $"{managementObject["FreePhysicalMemory"]:N0} KB"; | ||
} | ||
|
||
|
||
using (var managementClass = new ManagementClass("Win32_Processor")) | ||
using (var managementObject = managementClass.GetInstances().OfType<ManagementObject>().FirstOrDefault()) | ||
{ | ||
if (managementObject == null) return; | ||
|
||
this.CPU = managementObject["Name"].ToString(); | ||
} | ||
|
||
this.DotNetVersion = DotnetVersion.GetVersion(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
this.ErrorMessage = ex.Message; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
if (!string.IsNullOrEmpty(this.ErrorMessage)) | ||
{ | ||
return $@"SystemEnvironment | ||
({this.ErrorMessage})"; | ||
} | ||
|
||
return $@"SystemEnvironment | ||
OS: {this.OS} | ||
OSVersion: {this.OSVersion} | ||
Architecture: {this.Architecture} | ||
Runtime: {this.DotNetVersion} | ||
CPU: {this.CPU} | ||
RAM (Total): {this.TotalPhysicalMemorySize} | ||
RAM (Free): {this.FreePhysicalMemorySize}"; | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.