-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: sus-taguchi-t <[email protected]>
- Loading branch information
1 parent
e8b9ae3
commit b136d89
Showing
40 changed files
with
2,427 additions
and
28 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Extreal.SampleApp.Holiday.App.AppUsage | ||
{ | ||
[SuppressMessage("Usage", "IDE1006")] | ||
public class AppUsageBase | ||
{ | ||
public string ClientId; | ||
public string UsageId; | ||
public string StageName; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,108 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
using Cysharp.Threading.Tasks; | ||
using Extreal.Core.Logging; | ||
using Extreal.SampleApp.Holiday.App.Config; | ||
using UnityEngine; | ||
using UnityEngine.Networking; | ||
|
||
namespace Extreal.SampleApp.Holiday.App.AppUsage | ||
{ | ||
public class AppUsageLogWriter : ILogWriter | ||
{ | ||
private readonly AppUsageConfig appUsageConfig; | ||
private readonly AppScope.AppStateProvider appStateProvider; | ||
private readonly ILogWriter defaultLogWriter; | ||
|
||
public AppUsageLogWriter( | ||
AppUsageConfig appUsageConfig, | ||
AppScope.AppStateProvider appStateProvider, | ||
ILogWriter defaultLogWriter = null) | ||
{ | ||
this.appUsageConfig = appUsageConfig; | ||
this.appStateProvider = appStateProvider; | ||
this.defaultLogWriter = defaultLogWriter ?? new UnityDebugLogWriter(); | ||
} | ||
|
||
private const string AppUsageCategory = nameof(AppUsage); | ||
|
||
public void Log(LogLevel logLevel, string logCategory, string message, Exception exception = null) | ||
{ | ||
if (!appUsageConfig.Enable) | ||
{ | ||
defaultLogWriter.Log(logLevel, logCategory, message, exception); | ||
return; | ||
} | ||
|
||
if (AppUsageCategory == logCategory) | ||
{ | ||
SendAppUsage(message); | ||
} | ||
else if (LogLevel.Error == logLevel) | ||
{ | ||
SendErrorLog(message, exception); | ||
} | ||
else | ||
{ | ||
defaultLogWriter.Log(logLevel, logCategory, message, exception); | ||
} | ||
} | ||
|
||
private void SendAppUsage(string jsonLogLine) => PostAsync(jsonLogLine).Forget(); | ||
|
||
[SuppressMessage("Usage", "CC0022"), SuppressMessage("Design", "CC0004"), SuppressMessage("Usage", "RS0030")] | ||
private async UniTask PostAsync(string jsonLogLine) | ||
{ | ||
var statusCode = default(long); | ||
var exception = default(Exception); | ||
try | ||
{ | ||
var body = ToPostBody(jsonLogLine); | ||
using var request = new UnityWebRequest(appUsageConfig.PushUrl, UnityWebRequest.kHttpVerbPOST) | ||
{ | ||
uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(body)), | ||
downloadHandler = new DownloadHandlerBuffer(), | ||
timeout = appUsageConfig.TimeoutSeconds | ||
}; | ||
request.SetRequestHeader("Content-Type", "application/json"); | ||
request.SetRequestHeader("X-Scope-OrgID", appUsageConfig.OrgId); | ||
await request.SendWebRequest(); | ||
statusCode = request.responseCode; | ||
} | ||
catch (Exception e) | ||
{ | ||
exception = e; | ||
} | ||
#if !HOLIDAY_PROD | ||
// Logging for debugging during development. | ||
// ELogger cannot be used because of the infinite loop. | ||
if (statusCode != 0) | ||
{ | ||
Debug.Log($"{nameof(AppUsageLogWriter)}:{statusCode}"); | ||
Debug.Log(jsonLogLine); | ||
} | ||
|
||
if (exception != null) | ||
{ | ||
Debug.Log(exception.Message + Environment.NewLine + exception.StackTrace); | ||
} | ||
#endif | ||
} | ||
|
||
private static readonly string JsonPostBody = | ||
"{\"streams\":[{\"stream\":{\"app\":\"@value@\"},\"values\":[[\"@epochNS@\",\"@logLine@\"]]}]}"; | ||
|
||
private static string ToPostBody(string jsonLogLine) => | ||
JsonPostBody | ||
.Replace("@value@", nameof(Holiday)) | ||
.Replace("@epochNS@", (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1000000).ToString()) | ||
.Replace("@logLine@", jsonLogLine.Replace("\"", "\\\"")); | ||
|
||
private void SendErrorLog(string message, Exception exception = null) | ||
{ | ||
var errorStatus = ErrorStatus.Of(message, exception?.Message, exception?.StackTrace, LogType.Error, appUsageConfig); | ||
SendAppUsage(AppUsageUtils.ToJson(errorStatus, appUsageConfig, appStateProvider.AppState)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.