Skip to content

Commit e7131f0

Browse files
committed
add logger
1 parent 8472d59 commit e7131f0

11 files changed

+213
-169
lines changed

StarSharksTool/ADBLogin.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SharpAdbClient;
1+
using Microsoft.Extensions.Logging;
2+
using SharpAdbClient;
23
using System;
34
using System.Collections.Generic;
45
using System.ComponentModel;
@@ -58,6 +59,7 @@ public void LoadAdbDevices()
5859
}
5960
catch (Exception e)
6061
{
62+
Global.GetLogger("ADBLogin").LogError(e, e.Message);
6163
MessageBox.Show($"异常:{e.Message}");
6264
}
6365
}

StarSharksTool/AccountManagement.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Caching.Distributed;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
34
using Nethereum.Hex.HexConvertors.Extensions;
45
using Nethereum.JsonRpc.Client;
56
using Nethereum.Signer;
@@ -10,6 +11,7 @@
1011
using NPOI.POIFS.Crypt;
1112
using NPOI.POIFS.FileSystem;
1213
using NPOI.XSSF.UserModel;
14+
using Serilog;
1315
using SharpAdbClient;
1416
using StarSharksTool.Contracts.ERC20;
1517
using StarSharksTool.Extensions;
@@ -84,6 +86,21 @@ private async Task ReloadAccount()
8486
await LoadAccount();
8587
}
8688

89+
private void DisableButton()
90+
{
91+
this.button2.Enabled = false;
92+
this.button3.Enabled = false;
93+
this.button1.Enabled = false;
94+
this.batchRent.Enabled = false;
95+
}
96+
private void EnableButton()
97+
{
98+
this.button2.Enabled = true;
99+
this.button3.Enabled = true;
100+
this.button1.Enabled = true;
101+
this.batchRent.Enabled = true;
102+
}
103+
87104
private async Task LoadAccount()
88105
{
89106

@@ -178,8 +195,9 @@ private async Task LoadAccount()
178195
}
179196
accountDetailInfo.Add(address, (sharkAccountInfo.Result, sharks.Result, seaBalanceOfFunctionReturn.Result, sssBalanceOfFunctionReturn.Result, bnbBalance.Result));
180197
}
181-
catch
198+
catch(Exception e)
182199
{
200+
Global.GetLogger("AccountManagement").LogError(e, e.Message);
183201
goto GET_RETRY;
184202
}
185203
});
@@ -330,7 +348,7 @@ private async Task LoadAccount()
330348
Clipboard.SetDataObject(bscAccount.Address);
331349
};
332350

333-
351+
334352

335353
var groupBox = new GroupBox();
336354
groupBox.Text = $"({alias}){bscAccount.Address[0..6]} **** {bscAccount.Address[^4..]}";
@@ -348,8 +366,9 @@ private async Task LoadAccount()
348366
Global.Accounts.Add(account);
349367
}
350368
}
351-
catch
369+
catch(Exception e)
352370
{
371+
Global.GetLogger("AccountManagement").LogError(e, e.Message);
353372
goto RETRY;
354373
}
355374
}
@@ -377,7 +396,7 @@ private async Task<AccountInfoModel> GetAccountInfo(string token)
377396
var content = await httpResponse.Content.ReadAsStringAsync();
378397
if (!httpResponse.IsSuccessStatusCode)
379398
{
380-
Console.WriteLine($"https://www.starsharks.com/go/auth-api/account/base 失败: {content}");
399+
Global.GetLogger("AccountManagement").LogError($"https://www.starsharks.com/go/auth-api/account/base 失败: {content}");
381400
throw new HttpRequestException();
382401
}
383402
var responseModel = JsonConvert.DeserializeObject<ResponseModel<AccountInfoModel>>(content);
@@ -400,7 +419,7 @@ private async Task<IList<SharkModel>> GetSharks(string token, string address)
400419
var content = await httpResponse.Content.ReadAsStringAsync();
401420
if (!httpResponse.IsSuccessStatusCode)
402421
{
403-
Console.WriteLine($"https://www.starsharks.com/go/auth-api/account/sharks 失败: {content}");
422+
Global.GetLogger("AccountManagement").LogError($"https://www.starsharks.com/go/auth-api/account/sharks 失败: {content}");
404423
throw new HttpRequestException();
405424
}
406425
var responseModel = JsonConvert.DeserializeObject<ResponseModel<PagenationModel<SharkModel>>>(content);
@@ -567,6 +586,7 @@ await Parallel.ForEachAsync(Global.Accounts, async (item, cancelToken) =>
567586
}
568587
catch (Exception e)
569588
{
589+
Global.GetLogger("AccountManagement").LogError(e, e.Message);
570590
MessageBox.Show(item.Alias + "授权失败,原因:" + e.Message);
571591
}
572592
}

StarSharksTool/AddAccount.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ private void Save_Click(object sender, EventArgs e)
4141
{
4242
appSettings.Accounts = new List<AccountInfo> { };
4343
}
44+
var privateKey = keyAndAlias[0];
45+
if (privateKey.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
46+
{
47+
privateKey = privateKey[2..];
48+
}
4449
appSettings.Accounts.Add(new AccountInfo { Alias = keyAndAlias[1].Trim(), PrivateKey = AESHelper.Encrypt(keyAndAlias[0].Trim()) });
4550
}
4651
File.WriteAllText(Global.SETTING_PATH, JsonConvert.SerializeObject(appSettings, Formatting.Indented));

StarSharksTool/BatchRentPage.Designer.cs

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StarSharksTool/BatchRentPage.cs

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Nethereum.Web3;
1+
using Microsoft.Extensions.Logging;
2+
using Nethereum.Web3;
23
using StarSharksTool.Contracts.ERC20;
34
using StarSharksTool.Models;
45
using StarSharksTool.Models.RentModels;
@@ -137,7 +138,8 @@ public BatchRentPage()
137138
await Task.Delay(3);
138139
MethodInvoker mi = new MethodInvoker(() =>
139140
{
140-
if (NeedRentAccounts.Count == 0) {
141+
if (NeedRentAccounts.Count == 0)
142+
{
141143
this.autoRent.Checked = false;
142144
this.checkBox1.Checked = false;
143145
}
@@ -163,66 +165,74 @@ private async void refreshBtn_Click(object sender, EventArgs e)
163165
}
164166
private async Task RefreshData()
165167
{
166-
var textboxPrice = Convert.ToInt32(priceTextbox.Text);
167-
var level = 1;
168-
if (level1.Checked)
169-
{
170-
level = 1;
171-
}
172-
else if (level2.Checked)
173-
{
174-
level = 2;
175-
}
176-
else if (level3.Checked)
177-
{
178-
level = 3;
179-
}
180-
var c = await Services.Service.GetMarketplace(textboxPrice, level, rentProxy.Text);
181-
var model = c.Data.Sharks;
182-
DataTable dt = new DataTable();
183-
dt.Columns.Add(new DataColumn("Id"));
184-
dt.Columns.Add(new DataColumn("Price"));
185-
List<Task> autoRentTask = new List<Task>();
186-
Random r = new Random();
187-
if (model != null)
168+
try
188169
{
189-
foreach (var item in model)
170+
171+
var textboxPrice = Convert.ToInt32(priceTextbox.Text);
172+
var level = 1;
173+
if (level1.Checked)
174+
{
175+
level = 1;
176+
}
177+
else if (level2.Checked)
178+
{
179+
level = 2;
180+
}
181+
else if (level3.Checked)
190182
{
191-
if (BlackIds.Contains(item.Attr.SharkId))
192-
continue;
193-
var price = (int)Convert.ToDouble(item.Sheet.RentExceptGain);
194-
for (int i = 0; i < NeedRentAccounts.Count; i++)
183+
level = 3;
184+
}
185+
var c = await Services.Service.GetMarketplace(textboxPrice, level, rentProxy.Text);
186+
var model = c.Data.Sharks;
187+
DataTable dt = new DataTable();
188+
dt.Columns.Add(new DataColumn("Id"));
189+
dt.Columns.Add(new DataColumn("Price"));
190+
List<Task> autoRentTask = new List<Task>();
191+
Random r = new Random();
192+
if (model != null)
193+
{
194+
foreach (var item in model)
195195
{
196-
if (this.autoRent.Checked == true && price <= textboxPrice && NeedRentAccounts.Count > 0)
196+
if (BlackIds.Contains(item.Attr.SharkId))
197+
continue;
198+
var price = (int)Convert.ToDouble(item.Sheet.RentExceptGain);
199+
for (int i = 0; i < NeedRentAccounts.Count; i++)
197200
{
198-
var shark = model[r.Next(0, model.Count - 1)];
199-
if (!Services.Service.RentedSharkIds.Contains(shark.Attr.SharkId))
201+
if (this.autoRent.Checked == true && price <= textboxPrice && NeedRentAccounts.Count > 0)
200202
{
201-
autoRentTask.Add(this.RentShark(shark.Attr.SharkId, price));
203+
var shark = model[r.Next(0, model.Count - 1)];
204+
if (!Services.Service.RentedSharkIds.Contains(shark.Attr.SharkId))
205+
{
206+
autoRentTask.Add(this.RentShark(shark.Attr.SharkId, price));
207+
}
202208
}
203209
}
210+
var dr = dt.NewRow();
211+
dr["Id"] = item.Attr.SharkId.ToString();
212+
dr["Price"] = item.Sheet.RentExceptGain;
213+
dt.Rows.Add(dr);
204214
}
205-
var dr = dt.NewRow();
206-
dr["Id"] = item.Attr.SharkId.ToString();
207-
dr["Price"] = item.Sheet.RentExceptGain;
208-
dt.Rows.Add(dr);
209215
}
210-
}
211216

212-
this.dataGridView1.DataSource = dt;
217+
this.dataGridView1.DataSource = dt;
213218

214-
if (dataGridView1.Columns.GetLastColumn(DataGridViewElementStates.None, DataGridViewElementStates.None).Name != "rentBtnColumn")
215-
{
216-
DataGridViewButtonColumn rentBtnColumn = new DataGridViewButtonColumn();
217-
rentBtnColumn.Text = "租他";
218-
rentBtnColumn.HeaderText = "操作";
219-
rentBtnColumn.Name = "rentBtnColumn";
220-
rentBtnColumn.DefaultCellStyle.NullValue = "租它";
221-
dataGridView1.Columns.Add(rentBtnColumn);
219+
if (dataGridView1.Columns.GetLastColumn(DataGridViewElementStates.None, DataGridViewElementStates.None).Name != "rentBtnColumn")
220+
{
221+
DataGridViewButtonColumn rentBtnColumn = new DataGridViewButtonColumn();
222+
rentBtnColumn.Text = "租他";
223+
rentBtnColumn.HeaderText = "操作";
224+
rentBtnColumn.Name = "rentBtnColumn";
225+
rentBtnColumn.DefaultCellStyle.NullValue = "租它";
226+
dataGridView1.Columns.Add(rentBtnColumn);
227+
}
228+
if (autoRentTask.Any())
229+
{
230+
Task.Run(async () => { await Task.WhenAll(autoRentTask); });
231+
}
222232
}
223-
if (autoRentTask.Any())
233+
catch (Exception e)
224234
{
225-
Task.Run(async () => { await Task.WhenAll(autoRentTask); });
235+
Global.GetLogger("BatchRentPage").LogError(e, e.Message);
226236
}
227237
}
228238

@@ -258,7 +268,8 @@ private async Task RentShark(int sharkId, int maxPrice, decimal gasPrice = 6)
258268
do
259269
{
260270
retry++;
261-
if (retry > 5) {
271+
if (retry > 5)
272+
{
262273
return;
263274
}
264275
if (NeedRentAccounts.Count == RentLock.Count)
@@ -293,7 +304,8 @@ private async Task RentShark(int sharkId, int maxPrice, decimal gasPrice = 6)
293304
if (maxDynamicGasPrice.Checked == true)
294305
{
295306
var maxDynamicGasPrice = Convert.ToDecimal(maxDynamicGasPriceTbx.Text);
296-
if (currentDynamicGasPrice > maxDynamicGasPrice) {
307+
if (currentDynamicGasPrice > maxDynamicGasPrice)
308+
{
297309
return;
298310
}
299311
}
@@ -330,31 +342,18 @@ private async Task RentShark(int sharkId, int maxPrice, decimal gasPrice = 6)
330342
RerenderRentHistory();
331343
}
332344

333-
//private async void approveContract_Click(object sender, EventArgs e)
334-
//{
335-
// var resp = await Services.Service.ApproveContract(this._accountModel.Account.Address);
336-
// if (resp)
337-
// {
338-
// MessageBox.Show("授权成功");
339-
// }
340-
// else
341-
// {
342-
// MessageBox.Show("授权失败");
343-
// }
344-
//}
345-
346345
private async void checkBox1_CheckedChanged(object sender, EventArgs e)
347346
{
348347
if (checkBox1.Checked == true)
349348
{
350-
//Task.Run(() =>
351-
//{
352349
try
353350
{
354351
await AutoRefresh();
355352
}
356-
catch { }
357-
//}).ConfigureAwait(false);
353+
catch (Exception ex)
354+
{
355+
Global.GetLogger("BatchRentPage").LogError(ex, ex.Message);
356+
}
358357
}
359358
}
360359
private async Task AutoRefresh()

StarSharksTool/Global.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Caching.Distributed;
2+
using Microsoft.Extensions.Logging;
23
using StarSharksTool.Models;
34
using System.Net;
45

@@ -23,6 +24,7 @@ internal static decimal GetGasPrice(int seaPrice)
2324
}
2425
return (DynamicGASPrice[seaPrice] + 0.001m);
2526
}
27+
2628
internal static List<AccountModel> Accounts { get; set; } = new List<AccountModel>();
2729

2830
internal const string SETTING_PATH = "./AppSettings.json";
@@ -82,5 +84,11 @@ internal static HttpClient HTTPCLIENT
8284
}
8385

8486
public static IDistributedCache? Cache { get; internal set; }
87+
public static ILoggerFactory? LoggerFactory { get; internal set; }
88+
89+
internal static ILogger GetLogger(string? fullName)
90+
{
91+
return LoggerFactory.CreateLogger(fullName);
92+
}
8593
}
8694
}

0 commit comments

Comments
 (0)