コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
//LogUtility.cs
public static class LogUtility
{
/// <summary>
/// 保存パス
/// </summary>
private static string _savePath = null;
/// <summary>
/// ログを出力する
/// </summary>
/// <param name="err">エラー内容</param>
public static void OutPutError(string err)
{
//保存パスを形成する
//保存パス=ドキュメントパス&アプリ名_yyyyMMdd.log
if (String.IsNullOrEmpty(_savePath))
{
_savePath = Common.GetDocumentPath();
if (!_savePath.EndsWith("/"))
{
_savePath += "/";
}
_savePath += Common.FileService.GetPackageName() + "_";
}
//ログ内容の成形
string msg = "---------------------------------------------------------------------------------------------" + System.Environment.NewLine +
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " Error " + System.Environment.NewLine +
err;
//Visual Studio 上でのログを出力する
System.Diagnostics.Debug.WriteLine(msg);
//ファイルログを出力する
DependencyService.Get<IFileService>().OutPutLog(_savePath + DateTime.Now.ToString("yyyyMMdd") + ".log", msg);
}
}
//Common.cs
public static class Common
{
/// <summary>
/// ドキュメントディレクトリを取得する
/// </summary>
/// <returns></returns>
public static string GetDocumentPath()
{
string path = DependencyService.Get<IFileService>().GetDocumentPath();
if (String.IsNullOrEmpty(path))
{
path = DependencyService.Get<IFileService>().GetDownloadPath();
}
return path;
}
}
//IFileService.cs
//DependencyServiceから利用する
public interface IFileService
{
string GetPackageName();
string GetDocumentPath();
string GetDownloadPath();
void OutPutLog(string path, string err);
}
//Android FileService.cs
using Android.Content;
using System;
using System.IO;
using Xamarin.Forms;
[assembly: Dependency(typeof(FileService))]
public class FileService : IFileService
{
public string GetPackageName()
{
Context context = Forms.Context;
var name = context.PackageManager.GetPackageInfo(context.PackageName, 0).PackageName;
return name;
}
public string GetDocumentPath()
{
try
{
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments);
return dir.AbsolutePath;
}
catch
{
return String.Empty;
}
}
public string GetDownloadPath()
{
try
{
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
return dir.AbsolutePath;
}
catch
{
return String.Empty;
}
}
public void OutPutLog(string path, string err)
{
//System.IOを利用する場合
//System.IO.FileStream fs = new FileStream(path, FileMode.Append);
//StreamWriter sw = new StreamWriter(fs);
//sw.WriteLine(err);
//sw.Close();
//fs.Close();
//sw = null;
//fs = null;
//こちらの方がNativeです。Java.IO
Java.IO.FileWriter fw = new Java.IO.FileWriter(path, true);
Java.IO.BufferedWriter bw = new Java.IO.BufferedWriter(fw);
bw.Write(err);
bw.NewLine();
bw.Close();
fw.Close();
bw = null;
fw= null;
}
}
//iOS FileService.cs
using System;
using System.IO;
using Foundation;
using Xamarin.Forms;
[assembly: Dependency(typeof(FileService))]
public class FileService : IFileService
{
public string GetPackageName()
{
string name = NSBundle.MainBundle.InfoDictionary["CFBundleDisplayName"].ToString();
return name.ToString();
}
public string GetDocumentPath()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
public string GetDownloadPath()
{
return Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
}
public void OutPutLog(string path, string err)
{
System.IO.FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(err);
sw.Close();
fs.Close();
sw = null;
fs = null;
}
}
using AppName.Services;
using Xamarin.Forms;
public partial class TestPage : ContentPage
{
void Test()
{
try
{
//省略
}
catch (Exception ex)
{
LogUtility.OutPutError(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。