Skip to content

Commit

Permalink
Merge pull request #3609 from LeoChen98/dev-sentry
Browse files Browse the repository at this point in the history
引入sentry组件取代Github Issue自动错误报告
  • Loading branch information
LeoChen98 committed Apr 16, 2024
2 parents d992ace + 4dc2191 commit 3300abd
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 73 deletions.
12 changes: 10 additions & 2 deletions BiliRaffle/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
51 changes: 51 additions & 0 deletions BiliRaffle/BiliRaffle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<LangVersion>10.0</LangVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -65,16 +67,58 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>DmCommons\libs\DmCommons.Http.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="QRCoder, Version=1.4.3.0, Culture=neutral, PublicKeyToken=c4ed5b9ae8358a28, processorArchitecture=MSIL">
<HintPath>..\packages\QRCoder.1.4.3\lib\net40\QRCoder.dll</HintPath>
</Reference>
<Reference Include="Sentry, Version=4.4.0.0, Culture=neutral, PublicKeyToken=fba2ec45388e2af0, processorArchitecture=MSIL">
<HintPath>..\packages\Sentry.4.4.0\lib\net462\Sentry.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.5.0.0\lib\net461\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Reflection.Metadata, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.5.0.0\lib\net461\System.Reflection.Metadata.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.6.0.0\lib\net461\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=6.0.0.8, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.6.0.8\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mobile" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -173,4 +217,11 @@
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Sentry.4.4.0\build\Sentry.targets" Condition="Exists('..\packages\Sentry.4.4.0\build\Sentry.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Sentry.4.4.0\build\Sentry.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Sentry.4.4.0\build\Sentry.targets'))" />
</Target>
</Project>
92 changes: 27 additions & 65 deletions BiliRaffle/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DmCommons;
using Sentry;
using System;
using System.Diagnostics;
using System.Management;
Expand All @@ -15,67 +16,38 @@ internal class Program
{
#region Private Methods

private static string GetCPUInfo()
[STAThread]
private static void Main(string[] args)
{
using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
{
using (ManagementObjectCollection moc = cimobject.GetInstances())
{
string strCpuID = null;
foreach (ManagementObject mo in moc)
{
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return strCpuID;
}
}
}
System.Windows.Application app = new System.Windows.Application();

private static string GetMainDriveId()
{
using (ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"))
//初始化统计组件
SentrySdk.Init(options =>
{
using (ManagementObjectCollection moc = mc.GetInstances())
{
string strID = null;
foreach (ManagementObject mo in moc)
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
return strID;
}
}
}
// A Sentry Data Source Name (DSN) is required.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
// You can set it in the SENTRY_DSN environment variable, or you can set it in code here.
options.Dsn = "https://acdc5324b9bd28eb137011a3b84c8f76@o4507091526352896.ingest.us.sentry.io/4507095501570048";
/// <summary>
/// 获得16位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string GetMD5_16(string input)
{
return GetMD5_32(input).Substring(8, 16);
}
// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
// We enable it here for demonstration purposes when first trying Sentry.
// You shouldn't do this in your applications unless you're troubleshooting issues with Sentry.
options.Debug = false;
/// 获得32位的MD5加密 </summary> <param name="input"></param> <returns></returns>
private static string GetMD5_32(string input)
{
MD5 md5 = MD5.Create();
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:X2}", data[i]);
}
return sb.ToString();
}
// This option is recommended. It enables Sentry's "Release Health" feature.
options.AutoSessionTracking = true;
// Enabling this option is recommended for client applications only. It ensures all threads use the same global scope.
options.IsGlobalModeEnabled = true;
// This option will enable Sentry's tracing features. You still need to start transactions and spans.
options.EnableTracing = true;
// Example sample rate for your transactions: captures 10% of transactions
options.TracesSampleRate = 0.1;
});

[STAThread]
private static void Main(string[] args)
{
System.Windows.Application app = new System.Windows.Application();
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
Expand Down Expand Up @@ -132,19 +104,9 @@ private static void Main(string[] args)
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

if (!ViewModel.Main.AsPlugin) User_Statistics();
app.Run(new MainWindow());
}

private static void User_Statistics()
{
string cpu = GetCPUInfo();
string drive = GetMainDriveId();
string info = GetMD5_16(cpu + drive);
string json = $"{{\"pid\":119,\"version\":{Assembly.GetExecutingAssembly().GetName().Version.Revision},\"token\":\"{info}\"}}";
Http.PostBody("https://cloud.api.zhangbudademao.com/public/User_Statistics", json, null, "application/json; charset=UTF-8");
}

#endregion Private Methods
}
}
4 changes: 2 additions & 2 deletions BiliRaffle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.21.23")]
[assembly: AssemblyFileVersion("1.0.21.23")]
[assembly: AssemblyVersion("1.0.22.24")]
[assembly: AssemblyFileVersion("1.0.22.24")]
[assembly: Guid("fca5c3d7-30b6-4224-a77f-5851314bd435")]
10 changes: 6 additions & 4 deletions BiliRaffle/Raffle.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DmCommons;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sentry;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -241,7 +242,7 @@ public static void Start(string urlText, int num, bool IsReposeEnabled, bool IsC
}
catch (Exception ex)
{
Github.Send(REPO, new ExceptionEx(ex.Message, ex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors }));
SentrySdk.CaptureException(new ExceptionEx(ex.Message, ex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors }));
}
}

Expand Down Expand Up @@ -436,11 +437,11 @@ public static async void StartAsync(string urlText, int num, bool IsReposeEnable
}
}

Github.Send(REPO, new ExceptionEx(aex.Message, aex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors, aex_detail }));
SentrySdk.CaptureException(new ExceptionEx(aex.Message, aex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors, aex_detail }));
}
catch (Exception ex)
{
Github.Send(REPO, new ExceptionEx(ex.Message, ex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors }));
SentrySdk.CaptureException(new ExceptionEx(ex.Message, ex, new object[] { urlText, num, IsReposeEnabled, IsCommentEnabled, OneChance, CheckFollow, Filter, FilterCondition, IsRepliesInFloors }));
}
}

Expand Down Expand Up @@ -1068,8 +1069,9 @@ private static CookieCollection GetCookies(string cookies, string domain = "api.
}
return public_cookie;
}
catch (Exception)
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
return null;
}
}
Expand Down
13 changes: 13 additions & 0 deletions BiliRaffle/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="QRCoder" version="1.4.3" targetFramework="net48" />
<package id="Sentry" version="4.4.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Collections.Immutable" version="5.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Reflection.Metadata" version="5.0.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Json" version="6.0.8" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>

0 comments on commit 3300abd

Please sign in to comment.