###前提・実現したいこと
タイトル通りですが
ファイルの出力もしたいし
コンソールにも流したいし
変数とかで取得もしたい
と、ログを入力した時に色々したいです。
そのやることを他から指定できるようにしたいです。
AddHandler的なものがあればよかったんですが見つかりません。
皆さんどうやってるんでしょうか?
それともUnityはこういうことができないんですか?
###該当のソースコード
Logger logger = new Logger(new ILogHandlerクラス())
複数指定するのはできませんでした。
loggerへHandlerをAddするのも見つかりません。
###試したこと
どうしても解決できないので今はハンドラーが複数のハンドラーを管理するようなものを試そうとしてますが躓いてます。
###補足情報(言語/FW/ツール等のバージョンなど)
Unity5.6.3
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
複数のハンドラーの登録については見つかりませんね...自分で管理しろということでしょうかね?
イベントを使えば複数のメソッドの登録・解除を簡単に管理できそうだったので試してみましたが、いかがでしょうか。
参考: イベント - C# によるプログラミング入門 | ++C++; // 未確認飛行 C
ハンドラーのスクリプト
C#
1using UnityEngine; 2 3public class ExtendedLogHandler : ILogHandler { 4 5 public delegate void LogFormatEventHandler(LogType logType, Object context, string format, params object[] args); 6 public delegate void LogExceptionEventHandler(System.Exception exception, Object context); 7 8 public event LogFormatEventHandler LoggingFormat; 9 public event LogExceptionEventHandler LoggingException; 10 11 public void LogFormat(LogType logType, Object context, string format, params object[] args) 12 { 13 if (this.LoggingFormat != null) 14 { 15 this.LoggingFormat(logType, context, format, args); 16 } 17 } 18 19 public void LogException(System.Exception exception, Object context) 20 { 21 if (this.LoggingException != null) 22 { 23 this.LoggingException(exception, context); 24 } 25 } 26}
動作確認用スクリプト
C#
1using UnityEngine; 2 3public class LogHandlerTest : MonoBehaviour { 4 5 private ILogger logger; 6 private ExtendedLogHandler logHandler; 7 8 private void Awake() 9 { 10 this.logHandler = new ExtendedLogHandler(); 11 this.logger = new Logger(this.logHandler); 12 } 13 14 private void Update() 15 { 16 if (Input.GetKeyDown(KeyCode.A)) 17 { 18 Debug.Log("Add LogFormat."); 19 this.logHandler.LoggingFormat += OnLogFormat; 20 } 21 else if (Input.GetKeyDown(KeyCode.B)) 22 { 23 Debug.Log("Add LogException."); 24 this.logHandler.LoggingException += OnLogException; 25 } 26 else if (Input.GetKeyDown(KeyCode.C)) 27 { 28 Debug.Log("Remove LogFormat."); 29 this.logHandler.LoggingFormat -= OnLogFormat; 30 } 31 else if (Input.GetKeyDown(KeyCode.D)) 32 { 33 Debug.Log("Remove LogException."); 34 this.logHandler.LoggingException -= OnLogException; 35 } 36 else if (Input.GetKeyDown(KeyCode.E)) 37 { 38 this.logger.LogException(new System.Exception("Exception!"), this); 39 } 40 else if (Input.GetKeyDown(KeyCode.L)) 41 { 42 this.logger.LogFormat(LogType.Log, this, "Log:{0}, {1}", System.DateTime.Now, Time.timeSinceLevelLoad); 43 } 44 } 45 46 private void OnLogFormat(LogType logType, Object context, string format, params object[] args) 47 { 48 Debug.Log("LogFormat"); 49 Debug.unityLogger.LogFormat(logType, context, format, args); 50 } 51 52 private void OnLogException(System.Exception exception, Object context) 53 { 54 Debug.Log("LogException"); 55 Debug.unityLogger.LogException(exception, context); 56 } 57}
投稿2017/10/04 21:00
総合スコア10807
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/05 08:17