いつもお世話になっております。
CPU使用率やメモリ使用量、現在時刻などを1分間隔でテキストに書き出すプログラムを24時間動かし続けてテキストのファイル名を当日の日付にしてデータを残そうとしています。
以前の質問では「パフォーマンスモニター」を使っての設定だったのですが、こちらも完成させておきたいと思い、皆様のお知恵をお借りに来ました。
現状では自分のPCで絶対パスでフォルダを指定してテキストファイルを作成、書込みしていたのですが、テキストを出力するフォルダが固定で記載されていてこれだと、別のサーバで動かしたい場合
自由にフォルダを変更できないので運用上、都合が悪い。可変にすべきということで
ネットで検索して「app.config」で出来るのではないかと思い手探りで書いてみたら
エラーはありませんが、警告が1件あり「要素‘startup’には無効な子要素‘appSetttings’が含まれています。必要とされる要素は‘Runtime,requiredRuntime’supportedRuntime,requiredRuntime´です」という表示が出ています。
使い方を理解できていないまま書いたので当然だとは思いますが
正しい使い方やこういう風にすれば便利だというようなことがあれば
ご教授をお願い致します。
Program.cs using System; using System.Threading; using System.Diagnostics; using System.IO; using System.Configuration; namespace CodeRecipe_Timer_CS { class Program { static void Main(string[] args) { using (PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total", true)) { AutoResetEvent autoEvent = new AutoResetEvent(false); TimerCallback tDelegate = new TimerCallback(GetCpuUsage); using (Timer oTimer = new Timer(tDelegate, pc, 0, 60000)) { autoEvent.WaitOne(-1, false); //Thread.Sleep(86400000); } } //Console.WriteLine("Timer を停止しました。終了するには何かキーを押してください。"); Console.Read(); } static void GetCpuUsage(object state) { string today = DateTime.Now.ToString("yyyyMMdd"); PerformanceCounter pc = state as PerformanceCounter; DateTime dtNow = DateTime.Now; System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess(); p.Refresh(); if (pc != null) { //Console.WriteLine(String.Format("現在のCPU使用率は {0:f}% です。" + dtNow.ToString(), pc.NextValue())); //Console.WriteLine("物理メモリ使用量: {0}", p.WorkingSet64); //Console.WriteLine("仮想メモリ使用量: {0}", p.VirtualMemorySize64); File.AppendAllText(ConfigurationManager.AppSettings["key1"] + today + ".txt", String.Format(dtNow.ToString() + " ")); File.AppendAllText(ConfigurationManager.AppSettings["key1"] + today +".txt", String.Format("現在のCPU使用率は {0:f}% です。", pc.NextValue())); File.AppendAllText(ConfigurationManager.AppSettings["key1"] + today + ".txt", String.Format(" 物理メモリ使用量: {0}", p.WorkingSet64)); File.AppendAllText(ConfigurationManager.AppSettings["key1"] + today + ".txt", String.Format(" 仮想メモリ使用量: {0}", p.VirtualMemorySize64) + Environment.NewLine); } } } }
App1.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <appSettings> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> <add key="key1" value="value1" /> <add key="key2" value="value2" /> <add key="key3" value="value3" /> </appSettings> </startup> </configuration>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/17 06:53
2019/04/17 07:03
2019/04/17 07:15